Django返回JSON,如何解析js中的JSON对象

2024-10-02 02:33:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我从django代码JSON返回,如下所示:

我的django代码:

objs = AccessInfo.objects.filter(~Q(weblink=''))
return HttpResponse(serializers.serialize('json', objs), mimetype="application/json")

我在前台得到的是:

enter image description here

我希望能够在前端用javascript迭代这些JSON对象,并在一些html中显示每个fields值。在

如果我alert(data),我得到[ Object, Object ]。在

我试过$.parseJSON(data),但我得到了:

^{pr2}$

我该怎么做?在

更新

我的js接收的数据如下:

$.ajax({
   url: '/get_book_access_downloads/',
   type: 'get',
   data: {bookid:bookid},
   dataType: 'json'
}).done(function(data){
   // to do
});

Tags: django代码jsondatagetreturnobjectsobject
1条回答
网友
1楼 · 发布于 2024-10-02 02:33:36

您的数据已被解析。您有一个JavaScript对象列表,不需要再次解析它。在

您使用jQuery来加载数据,当您在$.ajax()调用上使用dataType: 'json'时,jQuery会为您解析JSON响应;如果您省略了该参数,jQuery将自动检测到JSON响应,并按其mime类型为您解码。在

有关dataType选项,请参见^{} documentation

The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string).

[...]

"json": Evaluates the response as JSON and returns a JavaScript object.

您有一个包含2个结果的列表;每个结果都是一个JavaScript对象,带有键pkmodel和{},而{}值是另一个包含每个模型字段的对象。在

相关问题 更多 >

    热门问题