class JSONEncoderTime(JSONEncoder):
def default(self, obj):
if isinstance(obj, LocalProxy):
obj = obj._get_current_object()
if isinstance(obj, datetime):
objs = obj.astimezone(pytz.utc)
return objs.strftime('%Y-%m-%dT%H:%M:%SZ')
return JSONEncoder.default(self, obj)
在 Flask 中使用它的必要步骤见下:
from flask import Flask
app = Flask(__name__)
app.json_encoder = JSONEncoderTime
网友评论