python 複雜物件轉json

2021-10-05 06:37:08 字數 3545 閱讀 2960

python不常用, 出了岔子, 想將幾個實體轉成json, 但是因為實體列表裡的實體裡面套實體列表…直接使用json.dumps(obj)會出現一些錯誤, 只能給裡面的列表新增的時候呼叫object.__dict__, 但是外層使用object.__dict__就會報錯, 很迷, 大概就是裡面的元素無法序列化, 或者是某乙個屬性無法找到對應的"key".

# 其實這麼寫就可以了=.=

json.dumps(obj=self.students,

default=

lambda x : x.__dict__, sort_keys=

false

, indent=

2)

import json

class

course

:def

__init__

(self, coursenum, coursename, coursecredit)

: self.coursenum = coursenum

self.coursename = coursename

self.coursecredit = coursecredit

class

student

:def

__init__

(self, studentnum, studentname,

*courses)

: self.studentnum = studentnum

self.studentname = studentname

self.courses =

self.courses += courses

class

istudentresp

(object):

defaddstudent

(self, student)

:pass

defselectstudent

(self, studentnum)

:pass

defdeletestudent

(self, studentnum)

:pass

defaddcourse

(self, studentnum, course)

:pass

defselectcourse

(self, studentnum, coursenum)

:pass

defdeletecourse

(self, studentnum, coursenum)

:pass

class

studentresp

(istudentresp)

: students =

def__init__

(self)

:pass

defaddstudent

(self, student)

:def

selectstudent

(self, studentnum)

:for stu in self.students:

if studentnum == stu.studentnum:

return stu

defdeletestudent

(self, studentnum)

: self.students.remove(self.selectstudent(studentnum)

)def

addcourse

(self, studentnum, course)

: self.selectstudent(studentnum)

defselectcourse

(self, studentnum, coursenum)

:for stu in self.students:

if stu.studentnum == studentnum:

for course in stu.courses:

if course.coursenum == coursenum:

return course

defdeletecourse

(self, studentnum, coursenum)

: self.selectstudent(studentnum)

.courses\

.remove(self.selectcourse(studentnum, coursenum)

)def

show

(self)

:print

(json.dumps(obj=self.students,

default=

lambda x : x.__dict__, sort_keys=

false

, indent=2)

)# print(json.dumps(self.students))

if __name__ ==

"__main__"

: sp = studentresp(

) sp.addstudent(student(

"001"

,"001"

, course(

"a",

"a",2)

, course(

"a",

"a",2)

))sp.addstudent(student(

"002"

,"001"

, course(

"a",

"a",2)

, course(

"a",

"a",2)

))sp.addstudent(student(

"003"

,"001"

, course(

"a",

"a",2)

, course(

"a",

"a",2)

))sp.addstudent(student(

"004"

,"001"

, course(

"a",

"a",2)

, course(

"a",

"a",2)

))sp.addcourse(

"001"

, course(

"a",

"a",2)

) sp.addcourse(

"001"

, course(

"a",

"a",2)

) sp.addcourse(

"001"

, course(

"a",

"a",2)

) sp.addcourse(

"001"

, course(

"a",

"a",2)

) sp.show(

)

json轉物件 物件轉json

我們經常會用到json,所以在c 裡就會經常有物件or物件陣列轉json,json轉物件or物件陣列。ps 物件或者json可能是 or,也就是json屬性or json陣列形式,或者json物件屬性裡巢狀,巢狀屬性.無限級巢狀都可以實現。前提 我們定義的物件,或者巢狀物件 一定要和json格式結構...

json轉物件和物件轉json

將值存入物件,並將其轉為字串 new name setopinion setratify ok setgrade 一級審批 newarraylist add 將值轉為字串 new string jsonstring null trycatch exception e 將字串轉為物件,此方法可以取代第...

JSON轉物件 物件轉JSON JSONArray

1.物件轉json,屬性為空的不顯示 geofencecreatedto geofencedto new geofencecreatedto bean物件新增註解 jsoninclude jsoninclude.include.non empty 預設 屬性為預設值不序列化 屬性為 空 或者為 nu...