From 5b92175cf5351b8b18c284df3d4b14c4b47ab23d Mon Sep 17 00:00:00 2001 From: luckygalaxy666 <393628658@qq.com> Date: Thu, 25 Jul 2024 18:39:51 +0800 Subject: [PATCH] Provide Python scripts to process Json files You can use `python script/ProcessJson.py path/to/config.json` to execute, If you don't provide the file path, it will default to be `../config.json`. See #5. --- config.json | 5 +++++ script/ProcessJson.py | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 config.json create mode 100644 script/ProcessJson.py diff --git a/config.json b/config.json new file mode 100644 index 0000000..1181c53 --- /dev/null +++ b/config.json @@ -0,0 +1,5 @@ +{ + "name": "noname", + "sex": "nosex", + "age": 10 +} diff --git a/script/ProcessJson.py b/script/ProcessJson.py new file mode 100644 index 0000000..f197864 --- /dev/null +++ b/script/ProcessJson.py @@ -0,0 +1,20 @@ +import argparse +import json +from types import SimpleNamespace + +def loadJsonAsObject(file_path: str): + with open(file_path, 'r', encoding='utf-8') as file: + data = json.load(file) + + # transmit dictronary into object + return json.loads(json.dumps(data), object_hook=lambda d: SimpleNamespace(**d)) + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Process JSON file.") + parser.add_argument('file_path', nargs='?', default='../config.json', help="Path to the JSON file") + args = parser.parse_args() + a = loadJsonAsObject(args.file_path) + + print(f"name: {a.name}") + print(f"sex: {a.sex}") + print(f"age: {a.age}")