json 不能 dumps datetime 解决办法
backend.myviews.json_time.py
from datetime import date
import json
from datetime import datetime
class JsonCustomEncoder(json.JSONEncoder):
def default(self, value):
if isinstance(value, datetime):
return value.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(value, date):
return value.strftime('%Y-%m-%d')
else:
return json.JSONEncoder.default(self, value)
from backend.myviews.json_time import JsonCustomEncoder
json.dumps(ret, cls=JsonCustomEncoder)
ps:
如果是django程序的话:
django自带一个json序列化类
from django.core.serializers.json import DjangoJSONEncoder