JSON。在Django中simplejson返回的解析错误

时间:2022-12-04 19:34:32

I have a view page that currently has two columns of data shown, soon to be expanded to four. Each column contains the result of a QuerySet for that particular model.

我有一个视图页面,目前显示了两列数据,很快将扩展到四列。每个列包含该特定模型的查询集的结果。

Here's what I have in my views.py method:

这是我的观点。py方法:

if request.REQUEST["type"] == "text":
        client = Client.objects.get(client_name = request.REQUEST["search"])
        peerList = ClientPeers.objects.prefetch_related().filter(client = client.client)
        compList = ClientCompetitors.objects.prefetch_related().filter(client = client.client)

else:
    peerList = ClientPeers.objects.prefetch_related().filter(client = request.REQUEST["search"])
    compList = ClientCompetitors.objects.prefetch_related().filter(client = request.REQUEST["search"])

for peer in peerList:
    peerlst.append({"pid" : peer.parentorg.parentorg, "pname" : peer.parentorg.parentorgname})

for comp in compList:
    complst.append({"cid" : comp.parentorg.parentorg, "cname" : comp.parentorg.parentorgname})

lst.append(simplejson.dumps(peerlst))
lst.append(simplejson.dumps(complst))

return HttpResponse(simplejson.dumps(lst), mimetype = "text/json")

This allows me to send a 2D array of data to the browser in the format

这允许我以这种格式向浏览器发送一个2D数据数组

[ { //JSON }, { //JSON } ]

In my jQuery.ajax success function, I have

在我的jQuery。ajax成功函数

function handler(results) {
  var data = JSON.parse(results);

  for (var i = 0; i < data[0].length; i++)
    $("#available_peers").append("<li>" + data[0][i].pname + "</li>");

  for (var i = 0; i < data[1].length; i++)
    $("#available_competitors").append("<li>" + data[1][i].cname + "</li>");

Firebug shows that the GET request works and I can see the data in the response tab. However, the console prints out

Firebug显示GET请求有效,我可以在response选项卡中看到数据。然而,控制台输出

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
var data = JSON.parse(results)

This error disappears if I replace var data = JSON.parse(results) with

如果我用var data = JSON.parse(results)替换这个错误就会消失

var peers = JSON.parse(data[0]);
var comps = JSON.parse(data[1]);

Why does one method work but another doesn't?

为什么一种方法有效,而另一种方法无效?

1 个解决方案

#1


1  

The jQuery ajax() call will make an intelligent guess as to the returned data type. In your example, function handler(results), the results variable will already be a decoded JSON object, containing two items in an array. The reason that JSON.parse(data[0]) works, is that you have returned JSON encoded data as a string.

jQuery ajax()调用将对返回的数据类型进行智能猜测。在函数处理程序(结果)示例中,结果变量将已经是一个经过解码的JSON对象,在数组中包含两个条目。JSON.parse(data[0])工作的原因是,您将JSON编码的数据作为字符串返回。

Don't encode the individual list elements to JSON before placing in the output array:

在输入输出数组之前,不要将单个列表元素编码为JSON:

lst.append(peerlst)  # <-- Don't encode to JSON string here
lst.append(complst)

return HttpResponse(simplejson.dumps(lst), mimetype = "application/json") # <-- Single JSON encoding

#1


1  

The jQuery ajax() call will make an intelligent guess as to the returned data type. In your example, function handler(results), the results variable will already be a decoded JSON object, containing two items in an array. The reason that JSON.parse(data[0]) works, is that you have returned JSON encoded data as a string.

jQuery ajax()调用将对返回的数据类型进行智能猜测。在函数处理程序(结果)示例中,结果变量将已经是一个经过解码的JSON对象,在数组中包含两个条目。JSON.parse(data[0])工作的原因是,您将JSON编码的数据作为字符串返回。

Don't encode the individual list elements to JSON before placing in the output array:

在输入输出数组之前,不要将单个列表元素编码为JSON:

lst.append(peerlst)  # <-- Don't encode to JSON string here
lst.append(complst)

return HttpResponse(simplejson.dumps(lst), mimetype = "application/json") # <-- Single JSON encoding