python json处理
- json写入json文件
点击查看代码
import json
json_data = {
"name": "Tom",
"age": 18,
"score": {
"math": 98,
"chinese": 99
}
}
json_file = 'D:\\2022program\\test.json'
with open(json_file, 'w', encoding='utf-8') as fw:
json.dump(json_data, fw)
- 读取json文件
点击查看代码
json_file = 'D:\\2022program\\test.json'
with open(json_file, 'r', encoding='utf-8') as fw:
json_data = json.load(fw)
print(json_data)
#{'name': 'Tom', 'age': 18, 'score': {'math': 98, 'chinese': 99}}