json有四个方法供我们进行数据转换:
mydict = {'name': 'xiaoming', 'age': 18}
#json.dumps 实现python类型转化为json字符串
json_str = json.dumps(mydict)
#json.loads 实现json字符串转化为python的数据类型
my_dict = json.loads(json_str)
#json.dump 实现把python类型写入类文件对象
with open("temp.txt","w") as f:
json.dump(mydict,f,ensure_ascii=False,indent=2)
# json.load 实现类文件对象中的json字符串转化为python类型
with open("temp.txt","r") as f:
my_dict = json.load(f)
网友评论