实现代码如下:
代码如下:
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)