json操作

#json就是一个字符串
import json
d = {
        "error_code": 0,
        "stu_info": [
                {
                        "id": 314,
                        "name": "矿泉水",
                        "sex": "男",
                        "age": 18,
                        "addr": "北京市昌平区",
                        "grade": "摩羯座",
                        "phone": "18317155663",
                        "gold": 100
                },
                {
                        "id": 315,
                        "name": "矿泉水",
                        "sex": "女",
                        "age": 27,
                        "addr": "上海",
                        "grade": "摩羯座",
                        "phone": "18317155664",
                        "gold": 100
                }
        ]
}
with open('abc.json','w',encoding='utf-8') as fw:
#         # s = json.dumps(d,ensure_ascii=False,indent=8) #把字典转成json,字符串
#         # 1、中文的问题
#         # 2、美观的问题
#         # fw.write(s)

        json.dump(d,fw,indent=4,ensure_ascii=False)


with open('abc.txt',encoding='utf-8') as fr:
        # result = fr.read()
        # print('result,刚从文件里面读出来的',type(result))
        # dic = json.loads(result)
        # print('dic,转换完之后的',dic,type(dic))
        result = json.load(fr)
        print('result',type(result))

 

上一篇:python 序列化 pickle和json


下一篇:Python之文件操作