4

我有一个这样的 JSON 字符串

 var json =  '{ "Comments": 
    [
      { "Id" : 1,"Comment" : "Test comment","Name" : "Yogesh","Child" : 0},
      { "Id" : 2,"Comment" : "Test comment II","Name" : "Yogesh","Child" : 0}
    ] 
    }';

我正在尝试像这样迭代对象:

var parsedJSON = $.parseJSON(json);

var html = "";    
for (comment in parsedJSON.Comments) {
  html += "Id: " + comment.Id;
  html += "Comment: " + comment.Comment;
  html += "Name: " + comment.Name;
  html += "Child: " + comment.Child;
  html += "<br/>";
}

但是comment在 for 循环中,我的意思不是一个对象,01只是一个字符串,我如何迭代这个数组?

4

5 回答 5

6
var json = '{ "Comments": [{ "Id" : 1,"Comment" : "Test comment","Name" : "Yogesh","Child" : 0},{ "Id" : 2,"Comment" : "Test comment II","Name" : "Yogesh","Child" : 0}] }';

var parsedJSON = $.parseJSON(json), // jsonData should json
    html = "",
    comments =parsedJSON.Comments; // keeping reference of parsedJSON and its an Array

// Here key will give 0, 1 etc ie. index of array

for (var key in comments) {
    html += "Id: " + comments[key].Id;
    html += "Comment: " + comments[key].Comment;
    html += "Name: " + comments[key].Name;
    html += "Child: " + comments[key].Child;
    html += "<br/>";
}

演示

于 2012-07-28T14:03:04.413 回答
2

您似乎误解了for..in循环的工作原理。comment将迭代地成为数组的键。在任何情况下,您都不应该for..in在数组上使用,只能在对象上使用——即使这样也要小心。

var l = parsedJSON.Comments.length, i, comment;
for( i=0; i<l; i++) {
    comment = parseJSON.Comments[i];
    // do stuff with comment
}
于 2012-07-28T14:03:13.603 回答
0

你可以试试这个:

     var JsonData =  { "Comments":
        [
          { "Id" : 1,"Comment" : "Test comment","Name" : "Yogesh","Child" : 0},
          { "Id" : 2,"Comment" : "Test comment II","Name" : "Yogesh","Child" : 0}
        ]
        };

    var html = "";    
    for (var i = 0; i < JsonData.Comments.length; i++) {
      comment = JsonData.Comments[i];
      html += "Id: " + comment.Id;
      html += " Comment: " + comment.Comment;
      html += " Name: " + comment.Name;
      html += " Child: " + comment.Child;
      html += "<br/>";
    }

alert(html);
于 2012-07-28T14:06:26.830 回答
0

您可以使用$.each

var html = ""; 
$.each(parsedJSON.Comments, function(i, comment) {
   html += "Id: " + comment.Id;
   html += "Comment: " + comment.Comment;
   html += "Name: " + comment.Name;
   html += "Child: " + comment.Child;
   html += "<br/>";
}

或者,您可以使用$.map

var html = $.map(parsedJSON.Comments, function(comment, i) {
   return "Id: " + comment.Id +
          "Comment: " + comment.Comment +
          "Name: " + comment.Name +
          "Child: " + comment.Child + 
          "<br/>";
}).join('');
于 2012-07-28T14:07:59.760 回答
0

Comments 包含一个包含两个元素的数组。

{ "Comments" : [ { 1 }, { 2 }]  ...

所以你可以访问它

for (var i = 0, len = parsedJSON.Comments.length; i < len; i++) {
    html += "Id: " + parsedJSON.Comments[i].Id;
    html += "Comment: " + parsedJSON.Comments[i].Comment;
    html += "Name: " + parsedJSON.Comments[i].Name;
    html += "Child: " + parsedJSON.Comments[i].Child;
    html += "<br/>";
}
于 2012-07-28T14:08:38.063 回答