I have a JSON string like this
var json = '{ "Comments":
[
{ "Id" : 1,"Comment" : "Test ment","Name" : "Yogesh","Child" : 0},
{ "Id" : 2,"Comment" : "Test ment II","Name" : "Yogesh","Child" : 0}
]
}';
and I'm trying to iterate over the objects as such:
var parsedJSON = $.parseJSON(json);
var html = "";
for (ment in parsedJSON.Comments) {
html += "Id: " + ment.Id;
html += "Comment: " + ment.Comment;
html += "Name: " + ment.Name;
html += "Child: " + ment.Child;
html += "<br/>";
}
But here ment
in for loop bees 0
and 1
only, I mean not an object but just a string, how can I iterate over this array?
I have a JSON string like this
var json = '{ "Comments":
[
{ "Id" : 1,"Comment" : "Test ment","Name" : "Yogesh","Child" : 0},
{ "Id" : 2,"Comment" : "Test ment II","Name" : "Yogesh","Child" : 0}
]
}';
and I'm trying to iterate over the objects as such:
var parsedJSON = $.parseJSON(json);
var html = "";
for (ment in parsedJSON.Comments) {
html += "Id: " + ment.Id;
html += "Comment: " + ment.Comment;
html += "Name: " + ment.Name;
html += "Child: " + ment.Child;
html += "<br/>";
}
But here ment
in for loop bees 0
and 1
only, I mean not an object but just a string, how can I iterate over this array?
-
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. – Felix Kling Commented Jul 28, 2012 at 14:03
5 Answers
Reset to default 6var json = '{ "Comments": [{ "Id" : 1,"Comment" : "Test ment","Name" : "Yogesh","Child" : 0},{ "Id" : 2,"Comment" : "Test ment II","Name" : "Yogesh","Child" : 0}] }';
var parsedJSON = $.parseJSON(json), // jsonData should json
html = "",
ments =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 ments) {
html += "Id: " + ments[key].Id;
html += "Comment: " + ments[key].Comment;
html += "Name: " + ments[key].Name;
html += "Child: " + ments[key].Child;
html += "<br/>";
}
Demo
You seem to have misunderstood how for..in
loops work. ment
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, ment;
for( i=0; i<l; i++) {
ment = parseJSON.Comments[i];
// do stuff with ment
}
YOu can try this:
var JsonData = { "Comments":
[
{ "Id" : 1,"Comment" : "Test ment","Name" : "Yogesh","Child" : 0},
{ "Id" : 2,"Comment" : "Test ment II","Name" : "Yogesh","Child" : 0}
]
};
var html = "";
for (var i = 0; i < JsonData.Comments.length; i++) {
ment = JsonData.Comments[i];
html += "Id: " + ment.Id;
html += " Comment: " + ment.Comment;
html += " Name: " + ment.Name;
html += " Child: " + ment.Child;
html += "<br/>";
}
alert(html);
You can use $.each
:
var html = "";
$.each(parsedJSON.Comments, function(i, ment) {
html += "Id: " + ment.Id;
html += "Comment: " + ment.Comment;
html += "Name: " + ment.Name;
html += "Child: " + ment.Child;
html += "<br/>";
}
Alternatively, you could use $.map
:
var html = $.map(parsedJSON.Comments, function(ment, i) {
return "Id: " + ment.Id +
"Comment: " + ment.Comment +
"Name: " + ment.Name +
"Child: " + ment.Child +
"<br/>";
}).join('');
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/>";
}