gpt4 book ai didi

javascript - 使用 Django 将 JSON 转为 JS : SyntaxError: missing : after property id

转载 作者:行者123 更新时间:2023-11-28 06:13:05 25 4
gpt4 key购买 nike

我正在尝试将 JSON 文件放入脚本中。我似乎无法通过从文件系统提供它来获取它,因此我创建了一个将 JSON 数据返回到页面的 View ,如下所示:

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})

在我的 JS 中:

var j = {{ json|safe }};

当我查看 JS 的源代码时,它显示了以下格式的数据:

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

我读过的应该不是问题。我没有任何名为“id”的变量,但我在标题中收到指向提供的 JS 行的错误。

为什么会这样?另外,我如何在脚本中使用 JSON 中的对象?

最佳答案

使用simplejson解决:

import simplejson as json

其他一切都如上所述。这是因为内置的 json.dumps 返回一个 unicode 数组,例如:

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

使用 simplejson 时这应该不是问题。

关于javascript - 使用 Django 将 JSON 转为 JS : SyntaxError: missing : after property id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36205162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com