脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python字典与json转换的方法总结

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

2021-08-19 00:10宋宋大人 Python

在本篇文章里小编给大家整理的是一篇关于python字典与json转换的方法总结内容,有需要的朋友们可以学习下。

在python中json分别由列表和字典组成,本文主要介绍python中字典与json相互转换的方法。使用json.dumps可以把字典转成json字符串。使用json.loads可以把json字符串转为字典类型的数据。

1、字典转json

使用json.dumps

json.dumps是对python对象编码成json对象,可以把字典转成json字符串。

方法格式

?
1
2
#字典转换成json字符串
json.dumps(dict)

实例

?
1
2
3
4
5
6
7
8
# 创建字典
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字符串。

方法格式

?
1
2
#json字符串转换成字典
json.loads(json_str)

使用实例

?
1
2
3
4
5
6
7
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字符串相互转化的实例扩展

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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()

到此这篇关于python字典与json转换的方法总结的文章就介绍到这了,更多相关python中字典与json相互转换的方法内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.py.cn/jishu/jichu/22410.html

延伸 · 阅读

精彩推荐