{"id":1568,"date":"2021-12-03T14:39:07","date_gmt":"2021-12-03T14:39:07","guid":{"rendered":"https:\/\/www.ipcpu.com\/?p=1568"},"modified":"2022-02-22T15:49:07","modified_gmt":"2022-02-22T15:49:07","slug":"datetime-datetime-is-not-json-serializable","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2021\/12\/datetime-datetime-is-not-json-serializable\/","title":{"rendered":"\u89e3\u51b3datetime.datetime is not JSON serializable"},"content":{"rendered":"

Object of type datetime is not JSON serializable<\/p>\n

\n
#\u89e3\u51b3datetime.datetime is not JSON serializable\nclass JsonDateEncoder(json.JSONEncoder):\n    def default(self, obj):\n        if isinstance(obj, datetime):\n            return obj.strftime('%Y-%m-%d %H:%M:%S')\n        elif isinstance(obj, date):\n            return obj.strftime(\"%Y-%m-%d\")\n        else:\n            return json.JSONEncoder.default(self, obj)\n\nallcontent = {xxx}\nprint(json.dumps({'status': 200, 'data': allcontent}, cls=JsonDateEncoder)<\/code><\/pre>\n<\/div>\n
\u6765\u81ea\u4e3a\u77e5\u7b14\u8bb0(Wiz)<\/a><\/div>\n

\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » \u89e3\u51b3datetime.datetime is not JSON serializable<\/a><\/p>","protected":false},"excerpt":{"rendered":"

Object of type datetime is not JSON serializable #\u89e3\u51b3datetime.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) \u6765\u81ea\u4e3a\u77e5\u7b14\u8bb0(Wiz) \u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def » \u89e3\u51b3datetime.datetime is not JSON serializable<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[59],"_links":{"self":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1568"}],"collection":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/comments?post=1568"}],"version-history":[{"count":1,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1568\/revisions"}],"predecessor-version":[{"id":1569,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1568\/revisions\/1569"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=1568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=1568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=1568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}