一,官方文档:
https://docs.python.org/zh-cn/3/library/json.html
二,json与字典的相互转化
1,字典转json字符串
1 2 3 4 5 6 7 |
import json
# 字典转json
d = dict (name = 'Tom' , age = 2 , score = 88 )
json_d = json.dumps(d)
print ( type (json_d))
print (json_d)
|
运行结果:
<class 'str'>
{"name": "Tom", "age": 2, "score": 88}
2,json字符串转字典
1 2 3 4 5 6 7 |
import json
# json转字典
json_c = '{"name": "Tom", "age": 2, "score": 88}'
c = json.loads(json_c)
print ( type (c))
print (c)
|
运行结果:
<class 'dict'>
{'name': 'Tom', 'age': 2, 'score': 88}
三,json与类实例的相互转化
1,通过dataclass把自定义类型的实例转为字典,再转为json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import json
from dataclasses import dataclass, asdict
@dataclass
class Student:
name: str
age: int
score: int
s = Student( 'Tom' , 2 , 88 )
print (s) # Student(name='Tom', age=2, score=88)
# 将自定义类转换为字典
custom_dict = asdict(s)
print ( type (custom_dict)) # <class 'dict'>
print (custom_dict) # {'name': 'Tom', 'age': 2, 'score': 88}
json_str = json.dumps(custom_dict)
print (json_str) # {"name": "Tom", "age": 2, "score": 88}
|
运行结果:
Student(name='Tom', age=2, score=88)
<class 'dict'>
{'name': 'Tom', 'age': 2, 'score': 88}
{"name": "Tom", "age": 2, "score": 88}
2,把实例通过__dict__转换为字典,再转为json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import json
class Student( object ):
def __init__( self , name, age, score):
self .name = name
self .age = age
self .score = score
s = Student( 'Tom' , 2 , 88 )
json_s = json.dumps(s, default = lambda obj: obj.__dict__)
print ( "__dict__转来的字典:" ,json_s) # {"name": "Tom", "age": 2, "score": 88}
json_s2 = json.dumps(s.__dict__)
print ( "__dict__转来的字典2:" ,json_s) # {"name": "Tom", "age": 2, "score": 88}
|
运行结果:
__dict__转来的字典: {"name": "Tom", "age": 2, "score": 88}
__dict__转来的字典2: {"name": "Tom", "age": 2, "score": 88}
3,通过定义函数把实例转为字典
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import json
class Student( object ):
def __init__( self , name, age, score):
self .name = name
self .age = age
self .score = score
# 定义把类转为字典的函数
def student2dict(std):
return {
'name' : std.name,
'age' : std.age,
'score' : std.score
}
s = Student( 'Tom' , 2 , 88 )
json_s = json.dumps(s, default = student2dict)
print (json_s)
|
运行结果:
{"name": "Tom", "age": 2, "score": 88}
4,通过定义函数把字典转为实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import json
class Student( object ):
def __init__( self , name, age, score):
self .name = name
self .age = age
self .score = score
def dict2student(d):
return Student(d[ 'name' ], d[ 'age' ], d[ 'score' ])
json_str = '{"name": "Tom", "age": 2, "score": 88}'
stu = json.loads(json_str, object_hook = dict2student)
print (stu) # <__main__.Student object at 0x104ea9460>
print ( type (stu)) # <class '__main__.Student'>
|
运行结果:
<__main__.Student object at 0x104ea9460>
<class '__main__.Student'>
说明:刘宏缔的架构森林—专注it技术的博客,
网址:https://imgtouch.com
本文: https://blog.imgtouch.com/index.php/2024/02/20/shi-yong-json-xu-lie-hua/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: [email protected]
四,用json实现对象深拷贝
1,可以用json实现对对象的深拷贝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def deep_copy(obj):
# 将对象转换为 JSON 格式的字符串
obj_str = json.dumps(obj)
# 从 JSON 字符串中重新加载对象
new_obj = json.loads(obj_str)
return new_obj
# 测试
original_list = [ 1 , 2 , { 'a' : 'b' }]
copied_list = deep_copy(original_list)
print ( "原始列表:" , original_list)
print ( "复制后的列表:" , copied_list)
|
运行结果:
原始列表: [1, 2, {'a': 'b'}]
复制后的列表: [1, 2, {'a': 'b'}]
标签:name,python,age,88,json,score,Tom,序列化
From: https://www.cnblogs.com/architectforest/p/18024153