4

I have a JSON string like this

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

and I'm trying to iterate over the objects as such:

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

But here comment in for loop becomes 0 and 1 only, I mean not an object but just a string, how can I iterate over this array?

1
  • 1
    You are not iterating over JSON, you are iterating over a JavaScript array. I corrected your question accordingly. You should use a for loop for that. You might also read about arrays in JavaScript. Commented Jul 28, 2012 at 14:03

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

Demo

Sign up to request clarification or add additional context in comments.

4 Comments

Giving code that doesn't work (ReferenceError: comments is not defined) with no explanation? Boo.
@Kolink: Perhaps a basic knowledge of correcting JS typos is assumed in the javascript tag :) I have changed the ";" to a "," to rectify the declaration error.
+1 for showing an interesting alternative to basic for-next indexing.
also, see this for a forEach varsion of this solution: jsfiddle.net/Jrg2k/5
2

You seem to have misunderstood how for..in loops work. comment will iteratively be the keys of the array. In any case, you should not use for..in on an array, only objects - and even then with caution.

var l = parsedJSON.Comments.length, i, comment;
for( i=0; i<l; i++) {
    comment = parseJSON.Comments[i];
    // do stuff with comment
}

1 Comment

I had tried it very first, but parsedJSON.Comments doesn't had any definition for length.
0

YOu can try this:

     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);

2 Comments

what didn't work exactly? because i tested it and it worked for me, here is my test: jsfiddle.net/VYTpG
Actually you didn't get the question right, I wasn't talking about a JSON object that you have, I was talking about a JSON string see this jsfiddle.net/VYTpG/1 but anyways thanx for help
0

You can use $.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/>";
}

Alternatively, you could use $.map:

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

Comments

0

Comments holds an array with two elements.

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

So you can access it with

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.