import json a={"name":"老王"} print(type(a)) # 字典转化为json b_json =json.dumps(a,ensure_ascii=False) print(type(b_json)) print(b_json) # 转化为字典 c_dict=json.loads(b_json) print(c_dict) print(type(c_dict))
a={"name":"路遥","book":"平凡的世界"} # 把Python的类型写入到类文件对象中 with open("file.txt","w",encoding="utf-8") as f: json.dump(a,f,ensure_ascii=False,indent=2)# fp参数指的是类文件对象,具有rend()或write()方法的对象。 #把类文件对象转化为python 的类型 with open("file.txt","r") as f: json.load(f)