在用测试网页获取后端数据时报了一个错:
TypeError: datetime.datetime(2018, 8, 18, 19, 47, 39, 171000) is not JSON serializable
这是因为JSON没有默认的日期时间类型,因此这就是Python无法自动处理它的原因。所以需要以某种方式将日期时间变成字符串。
第一种解决方法:编写一个自定义处理程序来帮助json模块
import datetime
import json
def datetime_handler(x):
if isinstance(x,datetime.datetime):
return x.isoformat()
raise TypeError("Unknown type")
json.dumps(data,default=datetime_handler())
第二种解决方法(so easy):就是将数据转换为字符串。这样,就可以用json转储了
kline.date = str(datetime.now())