我如何遍历这个 JSON 对象?

How do I loop through this JSON object?

main.php jQuery 代码:

$.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 个 post 和 3 个评论。 Post 和 id: 5 有 1 条评论,post 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"
}]}

(新 - 不适用于当前的 jQuery 代码)

示例对象包含 2 个 post 和 3 个评论。 Post 和 id: 5 有 1 条评论,post id: 2 有 2 条评论。

// 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 加上基本的 'for (var post in data.posts)' 循环:

var data = {
  "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"
    }]
  }
};


for (var post in data.posts) {
  $.each(data.comments[data.posts[post].id], function() {
    alert("Post: " + data.posts[post].id + ", Comment ID: " + this.id + ', Comment Text: "' + this.comment + '"')
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

第一个选项(vanilla JS):

var postObj;
for (var id in data.posts) {
  postObj = data.posts[id];
  // do your thing
}

var commentList;
for (var id in data.comments) {
  commentList = data.comments[id];
  commentList.forEach(function(comment) {
    // do your thing
  });
}

有关 for...in 循环的更多信息 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in


第二个选项(jQuery):

$.each(data.posts, function(id, post) {
  // do your thing
});

$.each(data.comments, function(id, commentList) {
  $.each(commentList, function(index, comment) {
    // do your thing. you could also use the forEach loop if you want
  });   
});

有关 $.each 的更多信息 http://api.jquery.com/jquery.each/

好吧,我说你需要 3 个循环来获取每个 comment 对象,例如

DEMO

$(data.comments).each(function(index,commentArray){ 
    $.each(commentArray,function(index,value){
        $.each(value,function(index1,value1){
            console.log(value1);
        });
    });
});

对于Post你可以用一个循环得到它

$.each(data.posts,function(index,post){
        console.log(post);
});