在python中json分别由列表和字典组成,本文主要介绍python中字典与json相互转换的方法。使用json.dumps可以把字典转成json字符串。使用json.loads可以把json字符串转为字典类型的数据。
1、字典转json
使用json.dumps
json.dumps是对python对象编码成json对象,可以把字典转成json字符串。
方法格式
#字典转换成json字符串 json.dumps(dict)
实例
# 创建字典 info_dict = {'name': 'Joe', 'age': 20, 'job': 'driver'} # dumps 将数据转换成字符串 info_json = json.dumps(info_dict,sort_keys=False, indent=4, separators=(',', ': ')) # 显示数据类型 print(type(info_json)) f = open('info.json', 'w') f.write(info_json)
2、json转字典
使用json.loads
json.loads是将json对象解码成python对象,即用于将字典类型的数据转成json字符串。
方法格式
#json字符串转换成字典 json.loads(json_str)
使用实例
In [25]: j Out[25]: '{"name": "mary", "age": 21}' In [26]: result = json.loads(j) In [27]: result Out[27]: {'name': 'mary', 'age': 21} In [28]: type(result) Out[28]: dict
python字典和json字符串相互转化的实例扩展
import json """ dumps:将python中的字典转换为字符串 output: {'fontFamily': '微软雅黑', 'fontSize': 12, 'BaseSettings': {'font': 1, 'size': {'length': 40, 'wigth': 30}}} {"fontFamily": "\u5fae\u8f6f\u96c5\u9ed1", "fontSize": 12, "BaseSettings": {"font": 1, "size": {"length": 40, "wigth": 30}}} """ def json_dumps(): json_dict = {'fontFamily': '微软雅黑', 'fontSize': 12, 'BaseSettings': {'font': 1, 'size': {'length': 40, 'wigth': 30}}} print(type(json_dict)) print(json_dict) json_str = json.dumps(json_dict) print(type(json_str)) print(json_str) """ dump:将数据写入json文件中 """ def json_dump(): json_dict = {'fontFamily': '微软雅黑', 'fontSize': 12, 'BaseSettings': {'font': 1, 'size': {'length': 40, 'wigth': 30}}} with open("../file/record.json", "w")as f: json.dump(json_dict, f) print("finished") """ loads:将字符串转换为字典 output: {"fontFamily": "微软雅黑", "fontSize": 12, "BaseSettings": {"font": 1, "size": {"length": 40, "wigth": 30}}} {'fontFamily': '微软雅黑', 'fontSize': 12, 'BaseSettings': {'font': 1, 'size': {'length': 40, 'wigth': 30}}} """ def json_loads(): json_str = '{"fontFamily": "\u5fae\u8f6f\u96c5\u9ed1", "fontSize": 12, "BaseSettings": {"font": 1, "size": {"length": 40, "wigth": 30}}}' print(type(json_str)) print(json_str) json_dict = json.loads(json_str) print(type(json_dict)) print(json_dict) """ load:读文件,并把字符串变换为Python数据类型 output: 40 {'fontFamily': '微软雅黑', 'fontSize': 12, 'BaseSettings': {'font': 1, 'size': {'length': 40, 'wigth': 30}}} """ def json_load(): f = open("../file/record.json", encoding='utf-8') setting = json.load(f) print(setting['BaseSettings']['size']['length']) setting['BaseSettings']['size']['length'] = 40 print(setting) if __name__ == '__main__': json_dumps() json_dump() json_loads() json_load()
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]
- 史依弘《史依弘交响乐京剧演唱专辑HQ》头版限量编号[WAV+CUE]
- 曼丽2020《女人三十风继续吹》HQCD[WAV+CUE]
- 曼丽2021《女人三十2雾之恋》HQCD[WAV+CUE]
- 隔壁老樊.2019-我曾【好听音乐】【FLAC分轨】
- 高林生.1993-牵挂你的人是我【白天鹅】【WAV+CUE】
- 洪卓立.2012-找个懂我的女孩(国专)【英皇娱乐】【WAV+CUE】
- 曼丽2022《女人三十3晚秋》HQCD[WAV+CUE]
- 曼丽2018《女人三十4冬天》HQCD[WAV+CUE]
- 柏菲-《好歌30年壹》限量开盘母带ORMCD[低速原抓WAV+CUE]
- 大自然音乐系列《水舞》[WAV/CUE/分轨][424.3MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]