python json模块默认不能序列化decimal和datetime数据,可以通过自定义一个序列化的类实现:
link: http://www.cnblogs.com/buxizhizhoum/p/7607036.html
import jsonimport 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)
时间: 2024-10-07 23:53:13