python datetime.datetime is not JSON serializable
1.主要是python list转换成json时对时间报错:datetime.datetime(2014, 5, 23, 9, 33, 3) is not JSON serializable。
2.解决方案:
1 import json 2 import datetime 3 4 class CJsonEncoder(json.JSONEncoder): 5 def default(self, obj): 6 if isinstance(obj, datetime.datetime): 7 return obj.strftime('%Y-%m-%d %H:%M:%S') 8 elif isinstance(obj, date): 9 return obj.strftime("%Y-%m-%d") 10 else: 11 return json.JSONEncoder.default(self, obj)
3.使用方法 :
json.dumps(yourdatetimeobj, cls=CJsonEncoder)
转自:https://blog.csdn.net/michael_star/article/details/26829149