I have an array
var data = [{"fname":"john","sname":"doe","id":"2"},{"country":"England"}];
I tried iterating with this:
var comment='';
for(var i = 0; i < data.length; i++) {
comment = data[i];
}
I access the data with this:
alert(comment.fname);
alert(comment.sname);
alert(comment.id);
alert(comment.country);
Only comment.country displays correctly, the rest show undefined.
$.parseJSON()to convert the string to an array.commentwill just contain the last element of the array. That element only hascountry, notfname,sname, orid.dataType: 'json'in the AJAX call, so that it would be parsed automatically? Why are you showing it as a string if it has already been parsed?$user[0]['country'] = 'England';.