How can I access this nested array within my JSON object?

Posted by Charles on Stack Overflow See other posts from Stack Overflow or by Charles
Published on 2010-06-17T04:14:04Z Indexed on 2010/06/17 4:23 UTC
Read the original article Hit count: 228

Filed under:
|
|
|

I'm using PHP to return a json_encode()'d array for use in my Javascript code. It's being returned as:

{"parent1[]":["child1","child2","child2"],"parent2[]":["child1"]}

By using the following code, I am able to access parent2 > child1

$.getJSON('myfile.php', function(data)
{
   for (var key in data)
   {
      alert(data[key]);
   }
}

However, this doesn't give me access to child1, child2, child, of parent1. Alerting the key by itself shows 'parent1' but when I try to alert it's contents, I get undefined.

I figured it would give me an object/array? How do I access the children of parent1?

data[key][0] ?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about JSON