首页 > Web开发 > 详细

json decimal and datetime

时间:2017-09-28 15:51:48      阅读:296      评论:0      收藏:0      [点我收藏+]

python json模块默认不能序列化decimal和datetime数据,可以通过自定义一个序列化的类实现:

link: http://www.cnblogs.com/buxizhizhoum/p/7607036.html

import json
import datetime


class
SupplementaryEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, decimal.Decimal): # for decimal return float(obj) elif isinstance(obj, datetime.datetime): # for datetime return obj.strftime("%Y-%m-%d %H:%M:%S") return json.JSONEncoder.default(self, obj)

test_dict = {"a": datetime.datetime.now()}
res = json.dumps(test_dict, cls=SupplementaryEncoder)

 

json decimal and datetime

原文:http://www.cnblogs.com/buxizhizhoum/p/7607036.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!