python: Serialize and Deserialize complex JSON using jsonpickle
# 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)
输出:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)