I have some JSON data being returned from an AJAX call. I then need to parse this data in javascript.
The data looks like so:
[
{
"id": "23",
"date_created": "2016-05-12 14:52:42"
},
{
"id": "25",
"date_created": "2016-05-12 14:52:42"
}
]
Why is it when i run this code on the data that i get multiple undefined's? (var json being the variable holding my json data)
for(var i = 0; i < json.length; i++) {
var obj = json[i];
console.log(obj.id);
}
However if i assign the json directly to the variable like so:
var json = [
{
"id": "23",
"date_created": "2016-05-12 14:52:42"
},
{
"id": "25",
"date_created": "2016-05-12 14:52:42"
}
];
Then it works fine!
Any ideas guys? Thanks
jsongetting a value from?var jsonthe returned json data?JSONyou're getting is not stringified JSON. In that case doJSON.parse(json_string)and then loop and more ...