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