解析jQuery中的JSON [英] Parse JSON in jquery
本文介绍了解析jQuery中的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试解析jquery中的以下json行:
I'm trying to parse the following json line in jquery:
[{
"pk": 19,
"model": "films.movies",
"fields": {
"length": "92",
"name": "Beetle Juice",
"actor": "Keaton",
"img_set": [{
"pk": 42,
"model": "films.img",
"fields": {
"uploaded": "2010-10-08 21:44:30",
"f_movie": 19,
"url_med": "http://www.mondial-infos.fr/wp-content/uploads/2009/10/Beetlejuice.jpg"}
}]
}
},{
"pk": 20,
"model": "films.movies",
"fields": {
"length": "126",
"name": "Batman",
"actor": "Keaton",
"img_set": [{
"pk": 43,
"model": "films.img",
"fields": {
"uploaded": "2010-10-08 21:44:54",
"f_movie": 20,
"url_med": "http://bruehoyt.com/superheroes/DC/batman/bruce/batmankeaton3.jpg"}
}]
}
}]
虽然img_set之后我无法访问任何内容.我想念什么?这是有效的json吗?
I can't access anything after img_set though. What am I missing? Is this valid json?
我正在尝试以下操作:
$.getJSON('/films/feeds/movie-by-actor/Keaton/',function(data) {
$.each(data, function(i, movie) {
alert(movie.fields.name);
alert(movie.fields.img_set[0].pk);
});
});
第一个警报起作用.第二个没有.
The first alert works. The second does not.
此外,尽管我不知道这很重要,但这是Django模板中的jquery.
In addition, though I don't know that it matters, this is jquery within a django template.
推荐答案
令人困惑的部分可能是单个元素的数组,但是img_set
仍然是数组.确保首先使用索引来访问它,如下所示:
The confusing part is probably the array of a single element, but img_set
is still an array. Make sure you're accessing it with an index first, like this:
.img_set[0].pk
//for example:
data[0].fields.img_set[0].pk
不只是:
.img_set.pk
这篇关于解析jQuery中的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文