当前位置:Gxlcms > Python > 让pythonjsonencodedatetime类型

让pythonjsonencodedatetime类型

时间:2021-07-01 10:21:17 帮助过:31人阅读

实现代码如下:

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)

更多让python json encode datetime类型相关文章请关注PHP中文网!

人气教程排行