import json
with open('data.json') as file:
res=file.read()
print(res)
# print(res["name"]) #无法直接获取
# print(type(res)) #dict->str 用str() 反过来dict()不行
res_dict=json.loads(res)#转化成字典
print(res_dict)
print(res_dict['name'])
res2=json.dumps(res_dict)#转化成字符串
print(res2)
print(type(res2))
在此项目的文件夹中创建data.json文件
内容如下
{"name":"marry","age":18}
最后的输出结果为:
"C:Program FilesAnaconda3python.exe" "D:/pycharm/test2/python gaoji.py" {"name":"marry","age":18} {'age': 18, 'name': 'marry'} marry {"age": 18, "name": "marry"} <class 'str'>