简体   繁体   English

如何遍历此JSON对象?

[英]How do I loop through this JSON object?

main.php jQuery code: 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
    })
})

(Old - Works with current jQuery code) (旧的-适用于当前的jQuery代码)

Example object containing 2 posts and 3 comments. 包含2个帖子和3个评论的示例对象。 Post with id: 5 has 1 comment and post id: 2 has 2 comments. 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"
}]}

(NEW - Does not work with current jQuery code) (新功能-不适用于当前的jQuery代码)

Example object containing 2 posts and 3 comments. 包含2个帖子和3个评论的示例对象。 Post with id: 5 has 1 comment and post id: 2 has 2 comments. 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"
        }]
    }
}

I'm not sure how to use this JSON object in this new scenario. 我不确定如何在这种新方案中使用此JSON对象。

Basically how do I loop through this new one? 基本上,我如何遍历这个新的?

You can easily iterate like this:- 您可以轻松地这样迭代:-

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

Comments can be looped through like the same way. 注释可以像以前一样循环遍历。

First option (vanilla JS): 第一选择(香草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 more info on for...in loops https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in 有关for ... in循环的更多信息,请参见https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...in


Second Option (jQuery): 第二种选择(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
  });   
});

For more info on $.each http://api.jquery.com/jquery.each/ 有关$ .each的更多信息, http: //api.jquery.com/jquery.each/

Try $.each coupled with a basic 'for (var post in data.posts)' loop: 尝试$ .each加上一个基本的'for(data.posts中的var post)'循环:

 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> 

Well I say you need 3 loops here to fetch each comment object like 好吧,我说您需要3个循环来获取每个comment对象,例如

DEMO 演示

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

for Post you can get it with a single loop 对于Post您可以通过一个循环获得它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM