简体   繁体   中英

I want to fetch a complex/nested json using jquery

Please, help me with fetching the JSON objects; I want to fetch name, poster.image.url, backdrops.image[1].size and version.

I am new to JSON, is there any easy way I can understand.

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

[
    {

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

DEMO

Thanks a lot Tshepang, you are awesome. 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.

I have done this ,

$(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 + '-' + backdrops.image.url+"
  • "); }); }); }); }); });

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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