JSON序列化类

 1 '''pyhton的dict对象可以直接序列化为JSON的{},不过很多时候
 2 我们更喜欢用class表示对象,比如定义Student类,然后序列化'''
 3 import json
 4 class Student(object):
 5      def __init__(self,name,age,score):
 6          self.name = name
 7          self.age = age
 8          self.score = score
 9 
10 def student2dict(std):
11     return{
12     'name':std.name,
13     'age':std.age,
14     'score':std.score
15     }
16 
17 s = Student('Bob',20,80)
18 print(json.dumps(s,default=student2dict))
原文地址:https://www.cnblogs.com/themost/p/6823857.html