码迷,mamicode.com
首页 > Web开发 > 详细

JSON序列化类

时间:2017-05-08 10:48:47      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:efault   code   __init__   对象   nbsp   sel   dump   std   imp   

 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))

 

JSON序列化类

标签:efault   code   __init__   对象   nbsp   sel   dump   std   imp   

原文地址:http://www.cnblogs.com/themost/p/6823857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!