简体   繁体   English

为什么 JSON.parse() 在我的代码中不起作用?

[英]why JSON.parse() is not working in my code?

I am using Django and trying to get data using javascript directly.我正在使用 Django 并尝试直接使用 javascript 获取数据。 Here are some codes.这里有一些代码。 In the idx_map.html, the JS part looks like this:在 idx_map.html 中,JS 部分是这样的:

 var act = '{{ activities_json }}';

    document.getElementById("json").innerHTML = act[0];

    var obj = JSON.parse(act);

    document.getElementById("demo").innerHTML = 1;

I am trying to find the bug so I put some codes to output lines to the page.我试图找到错误,所以我把一些代码输出到页面的行。 The problem is the JSON.parse() line.问题是JSON.parse()行。 If I comment that line, I can see [ and 1 in my HTML page, which means no bugs yet.如果我注释该行,我可以在我的 HTML 页面中看到 [ 和 1,这意味着还没有错误。 But if I uncomment the JSON.parse() line, I cannot see 1 anymore, indicating the JSON.parse() code has some error.但是如果我取消注释JSON.parse()行,我就看不到 1 了,这表明JSON.parse()代码有一些错误。 But I did not find anything wrong.但我没有发现任何错误。 Could someone help me with that?有人可以帮我吗?

In the views.py, I serialize the object into a json file.在views.py 中,我将对象序列化为一个json 文件。 The codes are:代码是:

def map(request):
activities_json = serializers.serialize("json", Activities.objects.all())
context = {
    "activities": Activities.objects.all(),
    "activities_json": activities_json,
}
return render(request, "CS_Activities/idx_map.html", context)

So the act should look like this (as a string?):所以行为应该是这样的(作为一个字符串?):

[
    {"model": "CS_Activities.activities", "pk": 1, "fields": {"act_name": "gun shot", "location": "York Universitty", "loc_lat": 43.76776, "loc_long": -79.50297, "time": "2018-11-05T20:25:08Z", "description": "a people dead"}}, {"model": "CS_Activities.activities", "pk": 2, "fields": {"act_name": "another gun shot", "location": "York Village", "loc_lat": 43.76, "loc_long": -79.5, "time": "2018-11-05T22:35:06Z", "description": "A person shot dead while walking"}}
]

Updates: In the idx_map.html, if I just replace the var act as the long string, there would be no errors.更新:在 idx_map.html 中,如果我只是将 var act 替换为长字符串,则不会出现错误。 So I think the problem in my case is how to retrieve the data from the Django database as a json file format.所以我认为我的问题是如何从 Django 数据库中以 json 文件格式检索数据。

You need to use safe which marks the string (or JSON in this case) as not requiring further HTML escaping prior to output.您需要使用safe来标记字符串(在本例中为 JSON),因为在输出之前不需要进一步的 HTML 转义。

var act = '{{ activities_json|safe }}';

This part of the code:这部分代码:

document.getElementById("json").innerHTML = act[0];

just gets the first element of the string, which is [.只获取字符串的第一个元素,即 [.

This part of the code这部分代码

var obj = JSON.parse(act);

returns error, because you didn't use safe previously and quote (") is output as &quote; in the template which is invalid. I guess you want something like this:返回错误,因为您之前没有使用safe并且引用 (") 在无效的模板中输出为&quote; ;。我想您想要这样的东西:

var act = '{{ activities_json|safe }}';
var lat = 43.767760;
var lng = -79.502970;
var latLng = {lat: lat, lng: lng};       

var obj = JSON.parse(act);
document.getElementById("json").innerHTML = obj[0];

Now obj[0] will return first JSON element, but I'm not sure what you want to do with it.现在obj[0]将返回第一个 JSON 元素,但我不确定你想用它做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM