Json中,datetime类型数据不能使用dumps直接转为字符串,需要在程序中添加如下代码:
class DateEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(obj, date):
return obj.strftime("%Y-%m-%d")
else:
return json.JSONEncoder.default(self, obj)
data = json.dumps(list, cls=DateEncoder)