使用 jQuery 访问 JSON 中的嵌套数组
我确信这是一个简单的问题,所以我提前为问这个问题表示歉意。在下面的代码中,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想迭代每个项目,jQuery 的
$.each()
可能能够帮助您实现目标。jsfiddle 上的代码示例。
If you want to iterate of each item jQuery's
$.each()
might be able to help you achieve your goal.Code example on jsfiddle.
就像在 Java 或 C# 中导航对象图一样,您可以执行
feeds.entries[0].link
来获取 Url 或feeds.entries[0].mediaGroups[0] .contents[0].fileSize
It is just like navigating the object graph in Java or C#, you can do
feeds.entries[0].link
to get the Url orfeeds.entries[0].mediaGroups[0].contents[0].fileSize