0

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" }
    ]
  }
]
  }
]

2 Answers 2

1

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:

$.getJSON('result.json', 
  function(data) {
    var dest = [];
    for (var i = 0; i < data.length; ++i) {
      for (var j = 0; j < data[i].dates.length; j++) {
        dates = data[i].dates[j];      
        visitors = data[i].dates[j].items[0];
        dest.push([dates.date,visitors.value]); 
      }
    }
    alert(dest);
  }
);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. For future reference...each time there is an nested array to loop through, a for statement needs to be included to access and loop through that array?
@Taylor: Yes, generally, but it depends. Your items arrays seem to always contain a single item, so you don't need a loop to access that item. On the other hand, if there is only ever one item, then you might want to change the data model so that it's not an array.
Thank you for the clarification. Usually there are multiple items. This particular call only yields one item.
0

This line is the problem

visitors = data[i].dates[i].items[i];

There's just one value in items so with the second iteration you're trying to read .items[1] which will fail.

Change the line to

visitors = data[i].dates[i].items[0];

2 Comments

I am still only receiving the first date back.
I've misread the data object because of the (strange) indentation... :\ But you've already got a working solution for your problem :)

Your Answer

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.