I am trying to parse a JSON response using getJSON, and having limited success parsing through the objects.
Here is the jquery that I am using, which is returning the appropriate values for the first date in the JSON. I need it to return all of the dates. Is there a particular reason why the following code will not do that?
$.getJSON('result.json',
function(data) {
var dest = [];
for (var i = 0; i < data.length; ++i) {
dates = data[i].dates[i];
visitors = data[i].dates[i].items[i];
dest.push([dates.date,visitors.value]);
}
alert(dest);
});
Here is the JSON:
[
{
"type": "visitors-unique",
"dates": [
{
"date": "2013-02-10",
"items": [
{ "value":"117" }
]
},
{
"date": "2013-02-09",
"items": [
{ "value":"427" }
]
},
{
"date": "2013-02-08",
"items": [
{ "value":"403" }
]
},
{
"date": "2013-02-07",
"items": [
{ "value":"227" }
]
},
{
"date": "2013-02-06",
"items": [
{ "value":"289" }
]
},
{
"date": "2013-02-05",
"items": [
{ "value":"246" }
]
},
{
"date": "2013-02-04",
"items": [
{ "value":"236" }
]
}
]
}
]
The problem is that you only have one loop when you have nested arrays. Your code will try to get the first date in the first queue, then the second date in the second queue, and so on. To get every date in every queue, you need nested loops: