簡體   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