python字典与json转换的方法总结

作者:宋宋大人 时间:2022-10-20 07:58:56 

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

来源:https://www.py.cn/jishu/jichu/22410.html

标签:python,字典,json
0
投稿

猜你喜欢

  • 详解mysql中的concat相关函数

    2024-01-16 06:36:22
  • ASP内置对象Request和Response用法详解

    2007-09-14 10:35:00
  • 用python建立两个Y轴的XY曲线图方法

    2023-06-30 15:01:26
  • 解决Django响应JsonResponse返回json格式数据报错问题

    2022-12-07 00:06:02
  • Go语言string,int,int64 ,float之间类型转换方法

    2023-06-28 15:20:30
  • 观点 2009 年,IE6 走好

    2009-01-04 16:46:00
  • PHP生成静态页面详解

    2023-11-21 06:50:43
  • MYSQL必知必会读书笔记 第一章(基础)

    2024-01-20 09:23:52
  • Javascript实现信息滚动效果

    2023-07-02 05:15:55
  • Python中多个数组行合并及列合并的方法总结

    2021-10-08 11:00:23
  • Bootstrap导航条学习使用(二)

    2024-05-02 17:31:16
  • ServerXMLHTTP的超时设置(setTimeouts)参数含义

    2009-02-12 12:51:00
  • 微信小程序跳一跳游戏 python脚本跳一跳刷高分技巧

    2023-01-31 22:30:44
  • python-pymongo常用查询方法含聚合问题

    2021-02-10 21:35:36
  • Mysql建库字符集和排序规则及说明

    2024-01-15 14:04:13
  • Python装饰器结合递归原理解析

    2023-07-13 22:24:00
  • python DataFrame转dict字典过程详解

    2022-08-16 20:59:08
  • go goth封装第三方认证库示例详解

    2024-02-11 14:05:35
  • Python实例练习水仙花数问题讲解

    2023-05-26 18:38:11
  • JavaScript让Textarea支持tab按键的方法

    2024-05-05 09:15:00
  • asp之家 网络编程 m.aspxhome.com