我想使用jquery获取一个复杂/嵌套的json

I want to fetch a complex/nested json using jquery

本文关键字:复杂 一个 嵌套 json jquery 获取      更新时间:2023-09-26

请帮助我获取JSON对象;我想获取名称,poster.image.url,backgrounds.image[1],大小和版本。

我是JSON的新手,有什么简单的方法可以理解吗。

我已经写了这个

$(document).ready(function() {
    $.getJSON("customer.json",function(data){
        $.each(data,function(key,value){
            $("ul").append("<li>"+ value.name+ value.posters.image.type +"</li>");
        });
    });
});

我真的坚持了下来。

我的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"
    }
]

您需要注意对象内部的数组。例如,以下是您如何访问第一张海报中的图像类型:

var imageType = data[0].posters[0].image.type;

有两个数组,所以每个循环需要两个:

$(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>");
            });
        });
    });
});

演示

非常感谢Tshepang,你太棒了。只是想问我是否必须显示backgrounds.image[2].url,所以我需要再次在里面制作$.each循环。我只是想了解这个循环需要如何完成。

我做到了,

$(document).ready(function(){$.getJSON("customer.json",function(customer){$.each(customer,function(key,customer