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)

 

相关文章:

  • 2021-08-08
  • 2021-05-24
  • 2021-06-16
  • 2022-12-23
  • 2021-12-25
  • 2022-12-23
  • 2021-06-14
  • 2021-10-26
猜你喜欢
  • 2021-12-06
  • 2022-01-17
  • 2021-10-27
  • 2021-09-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案