36

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..

Syscall
19.8k10 gold badges44 silver badges60 bronze badges
asked Dec 25, 2013 at 11:44
1
  • @KoIIIeY doesn't show any alert.. Commented Dec 25, 2013 at 11:49

8 Answers 8

74

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);
}); 
answered Dec 25, 2013 at 11:54
4
  • Anyway I can fade each? I used .hide().fadeIn(800); while appending, but it fades the whole thing at once, is it possible to fade 1 by 1? Commented Dec 25, 2013 at 13:22
  • You have to rebind event($(...).bind("") Or attachEvent()) after(before provide control to user) adding any element. Commented Dec 26, 2013 at 7:28
  • I figured it out, I hide all the divs then did a fade in on each, anyway thanks :D Commented Dec 27, 2013 at 5:26
  • Syntax error on the last line: needs correct close }); Commented Jan 6, 2015 at 6:06
13

Try this:

var data = jQuery.parseJSON(response);
$.each(data, function(key, item) 
{
 console.log(item.com);
});

or

var data = $.parseJSON(response);
$(data).each(function(i,val)
 {
 $.each(val,function(key,val)
 {
 console.log(key + " : " + val); 
 });
});
Ritesh
4974 silver badges15 bronze badges
answered Dec 25, 2013 at 11:48
1
  • Loops but shows [object, object] also I'm getting the json via ajax call Commented Dec 25, 2013 at 11:52
7

You are iterating through an undefined value, ie, com property of the Array's object, you should iterate through the array itself:

$.each(obj, function(key,value) {
 // here `value` refers to the objects 
});

Also note that jQuery intelligently tries to parse the sent JSON, probably you don't need to parse the response. If you are using $.ajax(), you can set the dataType to json which tells jQuery parse the JSON for you.

If it still doesn't work, check the browser's console for troubleshooting.

answered Dec 25, 2013 at 11:47
0
7
var data = [ 
 {"Id": 10004, "PageName": "club"}, 
 {"Id": 10040, "PageName": "qaz"}, 
 {"Id": 10059, "PageName": "jjjjjjj"}
];
$.each(data, function(i, item) {
 alert(data[i].PageName);
});​
$.each(data, function(i, item) {
 alert(item.PageName);
});​

Or else You can try this method

var data = jQuery.parseJSON(response);
$.each(data, function(key,value) {
 alert(value.Id); //It will shows the Id values
}); 
answered Oct 9, 2017 at 9:55
0
3
var data=[{'com':'something'},{'com':'some other thing'}];
$.each(data, function() {
 $.each(this, function(key, val){
 alert(val);//here data 
 alert (key); //here key
 });
});
answered Jul 31, 2014 at 13:08
2

you can get the key value pair as

<pre>
function test(){ 
var data=[{'com':'something'},{'com':'some other thing'}]; 
$.each(data, function(key,value) { 
alert(key); 
alert(value.com); 
}); 
}
</pre>
Sampada
3,0017 gold badges30 silver badges41 bronze badges
answered Apr 26, 2016 at 10:15
1
  • Please also add some explanation to your answer. Thanks! Commented Apr 26, 2016 at 10:36
2

Try this:

for(var i = 0; i < data.length; i++){
 console.log(data[i].com)
}
answered Apr 26, 2016 at 11:21
0

try this

var events = [];
alert(doc);
var obj = jQuery.parseJSON(doc);
 $.each(obj, function (key, value) {
 alert(value.title);

});

answered May 27, 2016 at 11:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.