如何遍历此JSON对象?

时间:2015-06-08 03:18:41

标签: javascript jquery json android-json

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个帖子和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"
}]}

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

包含2个帖子和3条评论的示例对象。 id: 5的帖子有1条评论,帖子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对象。

基本上我该如何循环使用这个新的?

4 个答案:

答案 0 :(得分:2)

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

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

评论可以通过相同的方式循环播放。

答案 1 :(得分:1)

第一个选项(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/

的更多信息

答案 2 :(得分:0)

尝试使用$ .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>

答案 3 :(得分:0)

我说你需要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);
});