datetime is not json serializable

python, datetime is not json serializable

import datetime

def json_serial(obj):
"""JSON serializer for objects not serializable by default json code""" if isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()
raise TypeError("Type %s not serializable" % type(obj))

示例代码:

from datetime import datetime
from json import dumps print dumps(datetime.now(), default=json_serial)

ref:https://*.com/questions/11875770/how-to-overcome-datetime-datetime-not-json-serializable

上一篇:mariadb 重置密码


下一篇:Android帧动画