JSON序列化datetime

from datetime import date,datetime
import json

d = {'name':'chao','birthday':datetime.now()}
# json_str = json.dumps(d)
# print(json_str)
class JsonCustomEncoder(json.JSONEncoder):
    def default(self, field):
        if isinstance(field,datetime):
            return field.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(field,date):
            return field.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self,field)

json_str = json.dumps(d,cls=JsonCustomEncoder)
print(json_str)

 

posted @ 2021-01-18 11:41  笑而不语心自闲  阅读(95)  评论(0编辑  收藏  举报