Dorami

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))

 

分类:

技术点:

相关文章:

  • 2022-01-29
  • 2021-10-07
  • 2022-02-06
  • 2021-06-05
  • 2021-12-30
  • 2022-01-27
猜你喜欢
  • 2021-11-23
  • 2021-10-09
  • 2021-07-17
  • 2021-08-23
相关资源
相似解决方案