Javascript 如何循环这个JSON对象?

Javascript 如何循环这个JSON对象?,javascript,jquery,json,android-json,Javascript,Jquery,Json,Android Json,main.phpjQuery代码: $.getJSON('posts.php',function(data){ data.posts.forEach(function(post){ // set variables and append divs to document }) data.comments.forEach(function(post){ // set variables and append divs to document

main.phpjQuery代码:

$.getJSON('posts.php',function(data){
    data.posts.forEach(function(post){
        // set variables and append divs to document
    })
    data.comments.forEach(function(post){
        // set variables and append divs to document
    })
})
(旧-使用当前jQuery代码)

包含2篇文章和3条评论的示例对象。id为5的帖子有1条评论,id为2的帖子有2条评论

// the two posts ID: 5 and 2

{"posts":[{
    "id":"5",
    "image":"link.jpg",
    "submitter":"4322309",
    "views":"3"
},
{
    "id":"2",
    "image":"link.jpg",
    "submitter":"4322309",
    "views":"10"
}],

// now each comment tied to the posts

"comments":[
{
    "id":"1",
    "submitter":"submitter",
    "time":"2435657",
    "comment":"comment",
    "score":"10",
    "postid":"2"
},
{
    "id":"2",
    "submitter":"submitter",
    "time":"2435657",
    "comment":"comment",
    "score":"10",
    "postid":"2"
},
{
    "id":"3",
    "submitter":"submitter",
    "time":"2435657",
    "comment":"comment",
    "score":"10",
    "postid":"5"
}]}
// the two posts ID: 5 and 2

{ 
    "posts":{
        "5": {
            "id":"5",
            "image":"link.jpg",
            "submitter":"4322309",
            "views":"3"
        },
        "2": {
            "id":"2",
            "image":"link.jpg",
            "submitter":"4322309",
            "views":"5"
        }
    },

    // now each comment tied to the posts
    "comments":{
        "2": [{
            "id":"1",
            "submitter":"submitter",
            "time":"2435657",
            "comment":"comment",
            "score":"10",
            "postid":"2"
        },
        {
            "id":"2",
            "submitter":"submitter",
            "time":"2435657",
            "comment":"comment",
            "score":"10",
            "postid":"2"
        }
    ],
        "5": [{
            "id":"3",
            "submitter":"submitter",
            "time":"2435657",
            "comment":"comment",
            "score":"10",
            "postid":"5"
        }]
    }
}
(新建-不适用于当前jQuery代码)

包含2篇文章和3条评论的示例对象。id为5的帖子有1条评论,id为2的帖子有2条评论

// the two posts ID: 5 and 2

{"posts":[{
    "id":"5",
    "image":"link.jpg",
    "submitter":"4322309",
    "views":"3"
},
{
    "id":"2",
    "image":"link.jpg",
    "submitter":"4322309",
    "views":"10"
}],

// now each comment tied to the posts

"comments":[
{
    "id":"1",
    "submitter":"submitter",
    "time":"2435657",
    "comment":"comment",
    "score":"10",
    "postid":"2"
},
{
    "id":"2",
    "submitter":"submitter",
    "time":"2435657",
    "comment":"comment",
    "score":"10",
    "postid":"2"
},
{
    "id":"3",
    "submitter":"submitter",
    "time":"2435657",
    "comment":"comment",
    "score":"10",
    "postid":"5"
}]}
// the two posts ID: 5 and 2

{ 
    "posts":{
        "5": {
            "id":"5",
            "image":"link.jpg",
            "submitter":"4322309",
            "views":"3"
        },
        "2": {
            "id":"2",
            "image":"link.jpg",
            "submitter":"4322309",
            "views":"5"
        }
    },

    // now each comment tied to the posts
    "comments":{
        "2": [{
            "id":"1",
            "submitter":"submitter",
            "time":"2435657",
            "comment":"comment",
            "score":"10",
            "postid":"2"
        },
        {
            "id":"2",
            "submitter":"submitter",
            "time":"2435657",
            "comment":"comment",
            "score":"10",
            "postid":"2"
        }
    ],
        "5": [{
            "id":"3",
            "submitter":"submitter",
            "time":"2435657",
            "comment":"comment",
            "score":"10",
            "postid":"5"
        }]
    }
}
我不确定在这个新场景中如何使用这个JSON对象


基本上,我如何循环使用这个新工具?

您可以像这样轻松地进行迭代:-

$.each(data['posts'], function(outerKey, idVal) {  //outerKyeas are 2, 5
   $.each(idVal, function(innerKey, val) {   // innerKeys are id,submitter etc
     console.log(innerKey, val);
  });
});

评论可以以相同的方式循环。

您可以像这样轻松迭代:-

$.each(data['posts'], function(outerKey, idVal) {  //outerKyeas are 2, 5
   $.each(idVal, function(innerKey, val) {   // innerKeys are id,submitter etc
     console.log(innerKey, val);
  });
});

评论可以以相同的方式循环。

您可以像这样轻松迭代:-

$.each(data['posts'], function(outerKey, idVal) {  //outerKyeas are 2, 5
   $.each(idVal, function(innerKey, val) {   // innerKeys are id,submitter etc
     console.log(innerKey, val);
  });
});

评论可以以相同的方式循环。

您可以像这样轻松迭代:-

$.each(data['posts'], function(outerKey, idVal) {  //outerKyeas are 2, 5
   $.each(idVal, function(innerKey, val) {   // innerKeys are id,submitter etc
     console.log(innerKey, val);
  });
});

注释可以以相同的方式循环。

Try$。每个注释都与基本的“for(var post in data.posts)”循环相结合:

var数据={
“员额”:{
"5": {
“id”:“5”,
“图片”:“link.jpg”,
“提交人”:“4322309”,
“视图”:“3”
},
"2": {
“id”:“2”,
“图片”:“link.jpg”,
“提交人”:“4322309”,
“视图”:“5”
}
},
//现在,每条评论都与帖子联系在一起
“评论”:{
"2": [{
“id”:“1”,
“提交人”:“提交人”,
“时间”:“2435657”,
“评论”:“评论”,
“得分”:“10分”,
“posted”:“2”
}, {
“id”:“2”,
“提交人”:“提交人”,
“时间”:“2435657”,
“评论”:“评论”,
“得分”:“10分”,
“posted”:“2”
}],
"5": [{
“id”:“3”,
“提交人”:“提交人”,
“时间”:“2435657”,
“评论”:“评论”,
“得分”:“10分”,
“posted”:“5”
}]
}
};
for(data.posts中的var post){
$.each(data.comments[data.posts[post].id],function()){
警报(“Post:“+data.posts[Post].id+”,注释id:“+this.id+”,注释文本:“+this.Comment+””)
});
}

试试$。每个都有一个基本的“for(data.posts中的var post)”循环:

var数据={
“员额”:{
"5": {
“id”:“5”,
“图片”:“link.jpg”,
“提交人”:“4322309”,
“视图”:“3”
},
"2": {
“id”:“2”,
“图片”:“link.jpg”,
“提交人”:“4322309”,
“视图”:“5”
}
},
//现在,每条评论都与帖子联系在一起
“评论”:{
"2": [{
“id”:“1”,
“提交人”:“提交人”,
“时间”:“2435657”,
“评论”:“评论”,
“得分”:“10分”,
“posted”:“2”
}, {
“id”:“2”,
“提交人”:“提交人”,
“时间”:“2435657”,
“评论”:“评论”,
“得分”:“10分”,
“posted”:“2”
}],
"5": [{
“id”:“3”,
“提交人”:“提交人”,
“时间”:“2435657”,
“评论”:“评论”,
“得分”:“10分”,
“posted”:“5”
}]
}
};
for(data.posts中的var post){
$.each(data.comments[data.posts[post].id],function()){
警报(“Post:“+data.posts[Post].id+”,注释id:“+this.id+”,注释文本:“+this.Comment+””)
});
}

试试$。每个都有一个基本的“for(data.posts中的var post)”循环:

var数据={
“员额”:{
"5": {
“id”:“5”,
“图片”:“link.jpg”,
“提交人”:“4322309”,
“视图”:“3”
},
"2": {
“id”:“2”,
“图片”:“link.jpg”,
“提交人”:“4322309”,
“视图”:“5”
}
},
//现在,每条评论都与帖子联系在一起
“评论”:{
"2": [{
“id”:“1”,
“提交人”:“提交人”,
“时间”:“2435657”,
“评论”:“评论”,
“得分”:“10分”,
“posted”:“2”
}, {
“id”:“2”,
“提交人”:“提交人”,
“时间”:“2435657”,
“评论”:“评论”,
“得分”:“10分”,
“posted”:“2”
}],
"5": [{
“id”:“3”,
“提交人”:“提交人”,
“时间”:“2435657”,
“评论”:“评论”,
“得分”:“10分”,
“posted”:“5”
}]
}
};
for(data.posts中的var post){
$.each(data.comments[data.posts[post].id],function()){
警报(“Post:“+data.posts[Post].id+”,注释id:“+this.id+”,注释文本:“+this.Comment+””)
});
}

试试$。每个都有一个基本的“for(data.posts中的var post)”循环:

var数据={
“员额”:{
"5": {
“id”:“5”,
“图片”:“link.jpg”,
“提交人”:“4322309”,
“视图”:“3”
},
"2": {
“id”:“2”,
“图片”:“link.jpg”,
“提交人”:“4322309”,
“视图”:“5”
}
},
//现在,每条评论都与帖子联系在一起
“评论”:{
"2": [{
“id”:“1”,
“提交人”:“提交人”,
“时间”:“2435657”,
“评论”:“评论”,
“得分”:“10分”,
“posted”:“2”
}, {
“id”:“2”,
“提交人”:“提交人”,
“时间”:“2435657”,
“评论”:“评论”,
“得分”:“10分”,
“posted”:“2”
}],
"5": [{
“id”:“3”,
“提交人”:“提交人”,
“时间”:“2435657”,
“评论”:“评论”,
“得分”:“10分”,
“posted”:“5”
}]
}
};
for(data.posts中的var post){
$.each(data.comments[data.posts[post].id],function()){
警报(“Post:“+data.posts[Post].id+”,注释id:“+this.id+”,注释文本:“+this.Comment+””)
});
}
第一个选项(香草JS):

有关For…in循环的详细信息


第二个选项(jQuery):

有关$each的更多信息,请参见第一个选项(vanilla JS):

有关For…in循环的详细信息


第二个选项(jQuery):

有关$each的更多信息,请参见第一个选项(vanilla JS):

有关For…in循环的详细信息


第二个选项(jQuery):

有关$each的更多信息,请参见第一个选项(vanilla JS):

更多信息