最新消息:

解决datetime.datetime is not JSON serializable

IT技术 ipcpu 698浏览 0评论

Object of type datetime is not JSON serializable

#解决datetime.datetime is not JSON serializable
class JsonDateEncoder(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)

allcontent = {xxx}
print(json.dumps({'status': 200, 'data': allcontent}, cls=JsonDateEncoder)
来自为知笔记(Wiz)

转载请注明:IPCPU-网络之路 » 解决datetime.datetime is not JSON serializable

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址