我在Google上搜索并找到了答案,但它对我不起作用。我必须像JsonResponse
在Django中一样发送一个列表,类似于:
list_to_json =[{"title": "hello there",
"link": "www.domain.com",
"date": ...},
{},{},{},...]
我通过应用StackOverflow的转换这JSON问题1和问题2,但它不是为我工作。我收到以下错误:
为了允许非字典对象被序列化,请将安全参数设置为False
这是我的代码:
def json_response(request):
list_to_json=[{"title": ..., "link": ..., "date": ...},{...}]
return JsonResponse(json.dumps(list_to_json) )
return JsonResponse(list_to_json, safe=False)
看一下文档:
安全的布尔参数默认为True。如果将其设置为False,则可以传递任何对象进行序列化(否则仅允许dict实例)。如果safe为True且将非字典对象作为第一个参数传递,则将引发TypeError。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句