开发者

Accessing Nested Arrays in JSON with jQuery

I'm sure this is a simple problem, so my apologies in advance for even asking it. In the code below, I'm converting an RSS feed to JSON using the jGFeed plugin. Within this JSON, I need to access a URL node that is nested several levels deep, inside a nested array. Right now, I'm able to use "console.log("feeds.entries[i]")" to get some attributes of each object at the parent level (i.e., "title", "content", etc.). I just can't figure out how to drill down into the nested array.

This is my code:

$(document).ready(function() {

    $.jGFeed('http://feeds.feedburner.com/solidverbal/app', function(feeds){
        // Check for errors
        if(!feeds){
        // there was an error
        return false;
        }
        var sAnchors = "<ul>";
        //var wantedFeed = "";
        for(var i=0; i<feeds.entries.length; i++){
            console.log(feed开发者_JAVA技巧s.entries[i]); 
            var sAnchors = sAnchors + "<li><a href=\"#link\" id=\"title_" + i + "\" class=\"giveFeeds\">" + feeds.entries[i].title + "</a></li>";
        }
        sAnchors = sAnchors + '</ul>';

        //Append the <a>.
        $('#titles').append(sAnchors);

        //When the user clicks on the giveFeeds link....
        $('.giveFeeds').live('click', function(e) {

            //Get the feed number
            var tmpId = $(e.target).attr("id");

            //Returns the value after the '_' char and make sure it returns de number.
            var id = parseInt(tmpId.split("_")[1]);

            //Use the id value to get the desired feed
            var wantedFeed = feeds.entries[id].content;

            //Then do whatever you want with that entry
            $("#content").html(wantedFeed);
        });


    }, 20);
});

Here is an example of the JSON structure as it appears within the Chrome console:

Object
     author: "Test"
     categories: Array[10]
     content: "<p></p><p>Test Content"
     contentSnippet: "Test Content Snippet"
     link: "http://www.solidverbal.com/2011/03/24/charles-robinson-324/"
     mediaGroups: Array[1]
          0: Object
               contents: Array[1]
                    0: Object
                         fileSize: "12796261"
                         type: "audio/mpeg"
                         url: "http://www.testurl/test.mp3"

Any help would be greatly appreciated. I'm thinking it's a nested for-loop or something, but for whatever reason I just can't get it.

Thanks in advance!


If you want to iterate of each item jQuery's $.each() might be able to help you achieve your goal.

$.each(feed.mediaGroups, function(x, mediaGroup) {

    $.each(mediaGroup, function(y, contents) {

        $.each(contents, function(z, content){
            alert(content.fileSize);
        });
    });
});

Code example on jsfiddle.


It is just like navigating the object graph in Java or C#, you can do feeds.entries[0].link to get the Url or feeds.entries[0].mediaGroups[0].contents[0].fileSize

for (var i = 0; i < feeds.entries.length; i++) {
    var entry = feeds.entries[i];
    for (var j = 0; j < entry.mediaGroups.length; j++) {
        var mediaGroup = entry.mediaGroup[j];
        for (var k = 0; k < mediaGroup.contents.length; k++) {
            var content = mediaGroup.contents[k];
            // do something with content.url.
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜