1 import json
2 # json.dumps()#把字典转成字符串
3 # json.load()#把字符串转换成字典
4 d = {
5 "xiaoming":"123456",
6 "xiaohei":"123456",
7 "张三":"111111",
8 "wangwu":"哈哈哈"
9 }
10 with open('a.json','w',encoding='utf-8') as fw:
11 # 方法1
12 # j = json.dumps(d,ensure_ascii=False,indent=4)#ensure_ascii=False:显示中文,indent=4:key前加4个空格
13 # fw.write(j)
14 # 方法2
15 json.dump(d,fw,ensure_ascii=False,indent=4)#不需要write()
16
17
18 with open('a.txt',encoding='utf-8') as fr:
19 # 方法1
20 # result = fr.read()
21 # str = json.loads(result)
22 # 方法2
23 str = json.load(fr)#自动读取,不需要手动read()
24 print(str)
25 print(type(str))