【发布时间】:2016-03-03 13:08:21
【问题描述】:
在返回 JSONResponse 时会发生这种情况,这是在 Django 1.7 中添加的。并且是json.dumps 的包装。但是,在这种情况下,它会导致错误。我确定数据是正确的,可以通过 Python shell 序列化为 JSON。
将数据序列化为 JSON 的正确方法是什么?
from django.http import JsonResponse
from collections import OrderedDict
data = OrderedDict([('doc', '546546545'), ('order', '98745'), ('nothing', '0.0')])
return JsonResponse(data) # doesn't work, JSONRenderer().render(data) works
导致此错误:
<map object at 0x7fa3435f3048> is not JSON serializable
print(data) 给出:
OrderedDict([('doc', '546546545'), ('order', '98745'), ('nothing', '0.0')])
【问题讨论】:
-
OrderedDict是您提供的示例数据还是您遇到错误的实际数据? -
就是这样,我省略了一些导致错误的数据。
-
<map object at 0x7...>在你的代码中做了什么? -
为了简化,我删除了一些部分,现在它被扩展了。
-
headers_prepared添加到输出中
标签: python json django python-3.x jsonresponse