Ram Singh
Ram Singh

Reputation: 6928

How to get third level of data in JSON response?

I want to get Flickr data from API:

I have used following API https://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=c87d0837c5bea1ac860102de95eee254&user_id=134029050%40N05&page=1&per_page=10&format=rest

My code is :

 <script type="text/javascript">
    (function () {
        var flickerAPI = "https://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=c87d0837c5bea1ac860102de95eee254&user_id=134029050%40N05&page=1&per_page=10&format=json&nojsoncallback=1";
        $.getJSON(flickerAPI, {
            tags: "mount rainier",
            tagmode: "any",
            format: "json"
        })
.done(function (data) {
    // debugger;
    $.each(data.photosets.photoset, function (i, item) {
        alert(data.photosets.photoset[i].title);

        var span = $('<span />').html(item[i]["title"]);
        $("#images").append(span);


    });
});
    })();
</script>

I want to get title of "photoset" element, i am able to get properties of "photoset", but i don't know how to fetch data inside "photoset".

Please help me.

Upvotes: 2

Views: 892

Answers (2)

Bhavin Solanki
Bhavin Solanki

Reputation: 4818

You just simply need to use : item.title._content to get the third level of json for the title

Please check my code to get result

(function () {
        var flickerAPI = "https://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=c87d0837c5bea1ac860102de95eee254&user_id=134029050%40N05&page=1&per_page=10&format=json&nojsoncallback=1";
        $.getJSON(flickerAPI, {
            tags: "mount rainier",
            tagmode: "any",
            format: "json"
        })
.done(function (data) {
    // debugger;
    $.each(data.photosets.photoset, function (i, item) {
        //console.log(data.photosets.photoset[i].title._content);
        alert(item.title._content);
        var span = $('<span />').html(item.title._content);
        $("#images").append(span);


    });
});
    })();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Ben Fried
Ben Fried

Reputation: 2204

The title is actually stored in _content inside of the title object. So you need to use:

data.photosets.photoset[i].title._content

Or inside of your loop, it would just be:

item.title._content

Fiddle here: https://jsfiddle.net/dho9xbt8/

Upvotes: 2

Related Questions