Niket Malik Niket Malik - 7 months ago 26
Javascript Question

how to loop through json array in jquery?

I have a php page from which I get response in json:

[{'com':'something'},{'com':'some other thing'}]


I want to loop it and append each to a div.

This is what I tried:

var obj = jQuery.parseJSON(response);
$.each(obj.com, function(key,value) {
alert(key+':'+value);
}


This alerts as
undefined
, and also response is the json array..

Please help.

Answer

Your array has default keys(0,1) which store object {'com':'some thing'} use:

var obj = jQuery.parseJSON(response);
$.each(obj, function(key,value) {
  alert(value.com);
});