【问题标题】:how to get json field value in template?如何获取模板中的json字段值?
【发布时间】:2012-06-15 19:58:41
【问题描述】:

我已经用我的模型对象生成了 json 并向模板发送了响应。 然后我想用 javascript 处理 json 获取我的模型的字段。 Python代码是这样的:

return json_serializer.serialize(queryset, ensure_ascii=False)

模板中的js在这里:

 function my_js_callback(data){
    alert(data) //1
    item=data[0]
    alert(item) //2
}

1 - 给我 [object Object], [object Object],... 2 - 给出 [object 对象]

还有这个:

alert(item['title'])
alert(item.title)
alert(item["title"])

都给我错误。如何获得我的头衔?

【问题讨论】:

  • JSON 是什么样的?

标签: python django json


【解决方案1】:

这是我的错。 我的 JSON 就像几个级别的字典,我找不到我的字段,因为它在 JSON 对象第一级字典中的字典命名字段中。 现在好了:

var key="title"
var item=data["fields"]
alert(item[key])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多