读取json
import jsonjson_file = 'test.json'with open(json_file,'r', encoding = 'utf-8') as load_f:data = json.load(load_f)print(data)
test.json
{"user_name": "test1","password" : "test2","id" : "1234567","file_list": ["f1", "f2","f3"]}
保存json
- 注意python3保存文件的时候默认
Unicode,需要在dump的时候ensure_ascii=False才可以,不然保存的仍然是Unicode。 - 使用
intend = 4可以设置保存json文件的时候缩进。 ```python import json
dic = {“user_name”: “test1”, “password” : “test2”, “id” : “1234567”, “file_list”: [“f1”, “f2”,”f3”]}
filename=’test.json’
with open(filename,’w’, encoding = ‘utf-8’) as file_obj: json.dump(dic, file_obj, ensure_ascii=False) ```
