[英]I want to fetch a complex/nested json using jquery
Please, help me with fetching the JSON objects; 请帮我获取JSON对象; I want to fetch name, poster.image.url, backdrops.image[1].size and version. 我想获取名称poster.image.url,backgrounds.image [1] .size和版本。
I am new to JSON, is there any easy way I can understand. 我是JSON的新手,有什么容易理解的方法。
I have written this 我写了这个
$(document).ready(function() {
$.getJSON("customer.json",function(data){
$.each(data,function(key,value){
$("ul").append("<li>"+ value.name+ value.posters.image.type +"</li>");
});
});
});
I really stuck with this. 我真的坚持了这一点。
My JSON FILE IS 我的JSON文件是
[
{
"name": "Masculin feminin",
"alternative_name": "Masculin féminin oder: Die Kinder von Marx und Coca Cola",
"posters": [
{
"image": {
"type": "poster",
"size": "thumb",
"height": 130,
"width": 92,
"url": "http://cf2.imgobject.com/t/p/w92/issm1E827fK7KHMEdRORA9BoTPs.jpg",
"id": "4ea5ebb234f8633bdc0020cb"
}
}
],
"backdrops": [
{
"image": {
"type": "backdrop",
"size": "thumb",
"height": 172,
"width": 300,
"url": "http://cf2.imgobject.com/t/p/w300/AnnWas1TyMRRyFuNT9bCZoeqg3t.jpg",
"id": "4ea5ebb734f8633bdc0020cf"
}
},
{
"image": {
"type": "backdrop",
"size": "poster",
"height": 448,
"width": 780,
"url": "http://cf2.imgobject.com/t/p/w780/AnnWas1TyMRRyFuNT9bCZoeqg3t.jpg",
"id": "4ea5ebb734f8633bdc0020cf"
}
},
{
"image": {
"type": "backdrop",
"size": "w1280",
"height": 736,
"width": 1280,
"url": "http://cf2.imgobject.com/t/p/w1280/AnnWas1TyMRRyFuNT9bCZoeqg3t.jpg",
"id": "4ea5ebb734f8633bdc0020cf"
}
},
{
"image": {
"type": "backdrop",
"size": "original",
"height": 768,
"width": 1336,
"url": "http://cf2.imgobject.com/t/p/original/AnnWas1TyMRRyFuNT9bCZoeqg3t.jpg",
"id": "4ea5ebb734f8633bdc0020cf"
}
}
],
"version": 463,
"last_modified_at": "2012-04-20 11:05:03 UTC"
}
]
You need to pay attention to the arrays inside the object. 您需要注意对象内部的数组。 For example, here's how you access the image type from the first poster: 例如,以下是从第一个海报访问图像类型的方法:
var imageType = data[0].posters[0].image.type;
There are two arrays so you need two each loops: 有两个数组,因此每个循环需要两个:
$(document).ready(function() {
$.getJSON("customer.json",function(data){
$.each(data,function(key,customer){
$.each(customer.posters,function(key,poster){
$("ul").append("<li>"+ customer.name + " - " + poster.image.type +"</li>");
});
});
});
});
Thanks a lot Tshepang, you are awesome. 非常感谢Tshepang,您真棒。 Just wanted to ask if i have to display the backdrops.image[2].url so do i again need to make the $.each loop inside.I just want to understand how this looping needs to be done. 只是想问问我是否必须显示backgrounds.image [2] .url,所以我还是需要在内部创建$ .each循环。我只想了解如何完成此循环。
I have done this , 我已经做到了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.