【发布时间】:2011-12-22 02:28:41
【问题描述】:
我正在尝试发送记录列表以响应 Ajax 查询。这很好用,除非当我的进程失败并出现错误 datetime.date(2011, 11, 1) is not JSON serializable 时结果包含日期时间字段。
我试图将我在此处找到的非常 similar question 的答案与 CherryPy documentation 中的说明结合起来使用自定义 json_out 编码器,但我不清楚该函数必须具有什么签名。我写的函数是:
def json_encoder(thing):
if hasattr(thing, 'isoformat'):
return thing.isoformat()
else:
return str(thing)
现在任何使用 json_out(即使输出中没有日期时间)给我错误TypeError: json_encoder() takes exactly 1 argument (0 given)。但是如果编码器不带参数,它如何接收要编码的对象呢?
(另外,我假设我使用str(thing) 作为默认编码方法是错误的,这应该通过调用 json 编码的默认处理程序来完成,但我不确定如何调用那个方法)。
【问题讨论】:
标签: python json datetime cherrypy