0

I have the following json data:

[{"id":"1","name":"vm"},{"id":"2","name":"live"}] 

Here's the code I have to parse:

$.getJSON('<%= path to my method%>',function(data) {
    console.log(data);  
    $.each(data, function(i, item) {                      
                   console.log(item[i].id);
           console.log(item[i].name);
    }); //end .each

});//end getJSON.       

Here are the results in the console:

LOG: [{"id":"1","name":"vm"},{"id":"2","name":"live"}] 
LOG: undefined 
LOG: undefined 
SCRIPT5007: Unable to get value of the property 'id': object is null or undefined 

I found this post: jquery loop on Json data using $.each

and so I tried to change my code to look like:

function(data) {
    console.log(data);  
    $.each(data, function(i, item) {
         console.log(data[i].id);
             console.log(data[i].name);
    }); //end .each

and this:

function(data) {
    console.log(data);  
    $.each(data, function(i, item) {
    console.log(item.id);
    console.log(item.name);
    }); //end .each

But I keep getting undefined for the ids and names.

Can you tell me where I'm going wrong please? Thanks.

EDIT 1

function(data) {
       $.parseJSON(data);
       console.log(data);  
       $.each(data, function(i, item) {
             console.log(item.id);
             console.log(item.name);
        }); //end .each
Community
  • 1
  • 1
mark li
  • 347
  • 1
  • 7
  • 22

4 Answers4

1

IIRC

$.each(data,function(i,item) 

should be

$.each(data,function(i)

then you can just use this.id and this.name inside the loop. Even if your solution turns out to be valid, it will still be faster to use mine, because going for data[i] will be slower than just using 'this'

Michiel Cornille
  • 2,067
  • 1
  • 19
  • 42
1

When you use .each, i is the index and item is the value. No need to to item[i], like you do in a for..in.

$.each(data, function(i, item) {                      
    console.log(item.id);
    console.log(item.name);
});
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0

Try this:

$.each(data, function() {
    console.log(this.id);
    console.log(this.name);
});
jRoB
  • 338
  • 1
  • 9
  • I think it is a moot point because, as one of the posters recognized, when the OP logs his json object it is coming through as a string. Shouldn't the console display it as [object][object] ? – jRoB Jun 12 '13 at 21:05
0

Your JSON is coming through as a string instead of a JSON object, therefore you're not able to access its properties.

Cast the string of JSON into an actual object by using $.parseJSON().

data = $.parseJSON(data);

The above should do the trick.

If your JSON source is potentially unreliable, I would suggest try/catch blocks to prevent invalid JSON from borking your javascript:

try {
    data = $.parseJSON(data);
} catch(errors) {
    console.log(errors);
}

Finally, if you control the source of the JSON, you can set the headers in the response to application/json and jQuery should automagically cast the response to a JSON object.

phpisuber01
  • 7,585
  • 3
  • 22
  • 26