Python 中如何读取/生成 json 数据


Python 中如何读取 / 生成 json 数据

在 Python 中对 json 数据读取生成操作,都需要先引入 json 模块

import json

读取 json 数据

从文件中读取

a_json = json.load(open('demo.json','r'))

从一个 str 中读取

a = json.loads('{'a':'1111','b':'2222'}')

Ps:Python 里面的 dict 与 json 长得很像不知道是不是巧合

生成 json 数据

生成 json 数据并保存至文件

a_dict = {'a':'1111','b':'2222'}
json.dump(a_dict, open('demo.json', 'w')

生成 json 数据并保存至变量

a_dict = {'a':'1111','b':'2222'}
a_str = json.dumps(a_dict) #将 json 数据存入一个字符串中
参考

https://www.cnblogs.com/bigtreei/p/10466518.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM