Transferred from: http://blog.csdn.net/leilba/article/details/50654256
1 #-*-coding:utf-8-*-2 ImportJSON3 4 #Custom Classes5 classMyClass:6 #Initialize7 def __init__(self):8self.a=29self.b='BB' Ten One ########################## A #Create a MyClass object -myclass=MyClass () - #Add Data C theMyclass.c=123 -Myclass.a=3 - #object into a dictionary -Myclassdict = MyClass.__dict__ + #Print Dictionary - Print(myclassdict) + #convert dictionaries to JSON AMyclassjson =json.dumps (myclassdict) at #Print JSON data - Print(Myclassjson) - - - ########################## - #json into a dictionary inMyclassrebuild =json.loads (Myclassjson) - #Print a rebuilt dictionary to Print(Myclassrebuild) + #Create a new MyClass object -Myclass2=MyClass () the #Convert A dictionary to an object *MyClass2.__dict__=Myclassrebuild; $ #print the reconstructed objectPanax Notoginseng Print(myclass2.a)
"Python" Python object and JSON convert each other