python封装json解析json读写json

tech2023-03-02  117

读json

with open("test.json",'r', encoding="utf-8") as load_f: # 读json并且转为dict load_dict = json.load(load_f)

写json

with open("test.json", "w", encoding='utf-8') as f: # json.dump(dict_var, f) # 写为一行 json.dump(dict_var, f,indent=2,sort_keys=True, ensure_ascii=False) # 写为多行

json字符串解析为字典

test = ‘{"key":"value"}’ # str 转json对象 json.loads(test)

json字典序列化为字符串

test = {"key":"value"} # 单行 json.dumps(test) # 多行 json.dumps(dict_var, indent=2,sort_keys=True, ensure_ascii=False)
最新回复(0)