python: Serialize and Deserialize complex JSON using jsonpickle

 

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# encoding: utf-8
# 版权所有 2024 ©涂聚文有限公司
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# Serialize and Deserialize complex JSON in Python
# 描述:pip install jsonpickle  https://github.com/jsonpickle/jsonpickle
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# Datetime  : 2024/11/19 20:33
# User      : geovindu
# Product   : PyCharm
# Project   : pyGenerator
# File      : main.py
# explain   : 学习
 
import json
import jsonpickle
from json import JSONEncoder
from dataclasses import dataclass
 
class Student(object):
    """
 
    """
    def __init__(self):
        """
 
        """
        self.firstname=None
        self.lastname=None
 
    @property
    def FirstName(self):
        """
 
        :return:
        """
        return self.firstname
    @FirstName.setter
    def FirstName(self,firstname:str):
        """
 
        :param firstname:
        :return:
        """
        self.firstname = firstname
 
    @property
    def LastName(self):
        """
 
        :return:
        """
        return self.lastname
 
    @LastName.setter
    def LastName(self,lastname:str):
        """
 
        :param lastname:
        :return:
        """
        self.lastname=lastname
 
 
 
class Team(object):
    """
 
    """
    def __init__(self):
        """
 
        :param students:
        """
        self.students = None
        self.teameName = None
 
 
    @property
    def Studnets(self) ->list[Student]:
        """
 
        :return:
        """
        return self.students
 
    @Studnets.setter
    def Studnets(self,students):
        """
 
        :param students:
        :return:
        """
        self.students=students
 
    @property
    def TeamName(self):
        """
 
        :return:
        """
        return self.teameName
 
    @TeamName.setter
    def TeamName(self,teamname:str):
        """
 
        :param teamname:
        :return:
        """
        self.teameName=teamname
 
class User:
    def __init__(self, id, name):
        self.id = id
        self.name = name
def user_decoder(obj):
    if 'id' in obj and 'name' in obj:
        return User(obj['id'], obj['name'])
    return obj
 
         
if __name__ == '__main__':
    """
     
    """
    teams=[]
    stu = []
    student1 = Student()
    student1.FirstName = "Geovin"
    student1.LastName = "Du"
    stu.append(student1)
    student2 = Student()
    student2.FirstName = "Sibo"
    student2.LastName = "Tu"
    stu.append(student2)
    team = Team()
    team.TeamName = "ICT"
    team.Studnets = stu
    teams.append(team)
    stu2=[]
    team = Team()
    student2 = Student()
    student1 = Student()
    student1.FirstName="聚文"
    student1.LastName="涂"
    stu2.append(student1)
    student2.FirstName="年生"
    student2.LastName="涂"
    stu2.append(student2)
    team.TeamName="HR"
    team.Studnets=stu2
    teams.append(team)
    print(teams)
     
    teamsJSON = jsonpickle.encode(teams)
    print(teamsJSON)
    with open('teams.json', "w") as outfile:
         print(teamsJSON)
         outfile.write(teamsJSON)
         outfile.close()
          
    teamsObject = jsonpickle.decode(teamsJSON)
    print("Object type is: ", type(teamsObject))
    for i in teamsObject:
        print("DEP:",i.teameName)
        for st in i.students:
            print(st.firstname,st.lastname)
       
    print("*********************")
    readjson=''
    with open('teams.json',mode="r", encoding="utf-8") as importfile:
        readjson = importfile.readline()
        print(readjson)
    print("*********************")
     
    teamsObject = jsonpickle.decode(readjson)
    for i in teamsObject:
        print("DEP:",i.teameName)
        for st in i.students:
            print(st.firstname,st.lastname)
     
    print("****************")
    json_data = '{"id": 1, "name": "John Doe"}'
    user = json.loads(json_data, object_hook=user_decoder)
    print(user.name)

  

输出:

 

posted @   ®Geovin Du Dream Park™  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示