报错:TypeError: Object of type 'datetime' is not JSON serializable
解决方式:
class CJsonEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, 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)
使用时候只要在json.dumps增加一个cls参数即可:
json.dumps(datalist, cls=CJsonEncoder)
转载链接:https://my.oschina.net/whp/blog/111173