简体   繁体   English

使用Django的JS的JSON:SyntaxError:missing:属性id之后

[英]JSON to JS with Django: SyntaxError: missing : after property id

I'm attempting to get a JSON file into a script. 我正在尝试将JSON文件放入脚本中。 I can't seem to be able to get it there by serving it from the filesystem so I made a view that returns the JSON data to the page like so: 我似乎无法通过从文件系统提供它来获得它,所以我创建了一个视图,将JSON数据返回到页面,如下所示:

def graph(request, d):                  #d.data is the file in the database
    data = json.load(d.data)
    return render(request, 'temp/template.html', {'json': data})

In my JS: 在我的JS中:

var j = {{ json|safe }};

When I look at the source for the JS it shows the data in this format: 当我查看JS的源代码时,它会以这种格式显示数据:

{u'people': [{u'name': u'steve'}, {u'name': u'dave'}]}

Which I read shouldn't be a problem. 我读的应该没问题。 I don't have any variables called 'id' and yet I get the error in the title pointing to the provided line of JS. 我没有任何名为'id'的变量,但我在标题中指出了JS提供的错误。

Why could this be? 为什么会这样? Also how do I then use the objects from the JSON in my script? 另外,如何在脚本中使用JSON中的对象?

Solved by using simplejson: 通过使用simplejson解决:

import simplejson as json

And everything else as above. 以及以上所有其他内容。 This is because the built in json.dumps returns an array of unicode like: 这是因为内置的json.dumps返回一个unicode数组,例如:

{u'people': [{u'name': u'steve'}, {u'name': u'dave'}]}

When using simplejson that shouldn't be a problem. 使用simplejson时应该不是问题。

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

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