Post

0402JSON

读取

1
2
3
4
5
6
import json
#1.loads 字符串
js=json.loads(str)

#2.load打开的文件
json.load(open('data.json', encoding='utf-8'))

存储

1
2
3
4
5
6
# 1
with open('data.json', 'w', encoding='utf-8') as file:
    file.write(json.dumps(data, indent=2)) #indent:格式化缩进2个数(默认不锁紧输出一行)。ensure_ascii:中文被显示成Unicode,将ensure_ascii=False

# 2.
json.dumps(data,open'data.json', 'w', encoding='utf-8'), indent=2)
This post is licensed under CC BY 4.0 by the author.