使用 jQuery 访问 JSON 中的嵌套数组

发布于 2024-10-27 19:58:29 字数 2120 浏览 1 评论 0原文

我确信这是一个简单的问题,所以我提前为问这个问题表示歉意。在下面的代码中,我使用 jGFeed 插件将 RSS 提要转换为 JSON。在此 JSON 中,我需要访问嵌套数组内嵌套了几层的 URL 节点。现在,我可以使用“console.log(“feeds.entries[i]”)”来获取父级每个对象的一些属性(即“标题”、“内容”等)。我只是不知道如何深入到嵌套数组。

这是我的代码:

$(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(feeds.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);
});

这是 Chrome 控制台中显示的 JSON 结构的示例:

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"

任何帮助将不胜感激。我认为这是一个嵌套的 for 循环或其他东西,但无论出于何种原因我就是无法得到它。

提前致谢!

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(feeds.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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我偏爱纯白色 2024-11-03 19:58:29

如果您想迭代每个项目,jQuery 的 $.each() 可能能够帮助您实现目标。

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

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

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

jsfiddle 上的代码示例。

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.

橘亓 2024-11-03 19:58:29

就像在 Java 或 C# 中导航对象图一样,您可以执行 feeds.entries[0].link 来获取 Url 或 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.
        }
    }
}

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.
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文