报错:TypeError: Object of type 'datetime' is not JSON serializable

  解决方式:

class CJsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, obj)

 使用时候只要在json.dumps增加一个cls参数即可:

json.dumps(datalist, cls=CJsonEncoder)

  转载链接:https://my.oschina.net/whp/blog/111173

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-12
  • 2022-12-23
  • 2021-08-31
  • 2022-12-23
  • 2021-08-29
猜你喜欢
  • 2021-07-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案