- 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)
- #该片段来自于http://www.codesnippet.cn/detail/010420132674.html
来源: http://www.codesnippet.cn/detail/010420132674.html