首页 > 编程 > Python > 正文

让python json encode datetime类型

2019-11-25 18:43:33
字体:
来源:转载
供稿:网友
实现代码如下:
复制代码 代码如下:

import json
from datetime import date, datetime


def __default(obj):
if isinstance(obj, datetime):
return obj.strftime('%Y-%m-%dT%H:%M:%S')
elif isinstance(obj, date):
return obj.strftime('%Y-%m-%d')
else:
raise TypeError('%r is not JSON serializable' % obj)

print json.dumps({'d': datetime.now(), 'today': date.today(), 'x': 111},
default=__default)
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表