1 python版
1.1 json 写入文件
import json
data = {'a': 1, 'b': 2}
with open("xxx.json", "w", encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, sort_keys=True, indent=2, separators=(',', ':'))
注意:保存数据中有中文时需要用 utf-8 的格式打开文件,并且 .dump 时设置 ensure_ascii=False
。
2 c版