- # 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
-
- 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("****************")
复制代码 输出:

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |