繁体   English   中英

我想使用jQuery获取复杂/嵌套的JSON

[英]I want to fetch a complex/nested json using jquery

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

我是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){$ .each(customer.posters,function(key ,posters){$ .each(customer.backdrops,function(key,backdrops){$(“ ul”)。append(“
  • “ + customer.name + posters.image.type +'-'+ backgrounds.image.url +”
  • “);});});});});});

    暂无
    暂无

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM