Itération sur le tableau d'objets

J'ai une chaîne JSON comme ce

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

et je suis en train de parcourir les objets en tant que tels:

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/>";
}

Mais ici comment en boucle devient 0 et 1 seulement, je veux dire, pas un objet, mais simplement une chaîne de caractères, comment puis-je effectuer une itération sur ce tableau?

  • Vous n'êtes pas à parcourir JSON, vous êtes de parcourir un tableau JavaScript. J'ai corrigé votre question en conséquence. Vous devez utiliser un for boucle pour cela. Vous pouvez également lire tableaux en JavaScript.
InformationsquelleAutor yogi | 2012-07-28