views:

625

answers:

2

Hey,

I'm having a problem parsing a JSON file in AS3. Im trying to parse multiple JSON arrays, but don't really know how to get to the next after accessing the first one. My JSON file looks like:

{   
    "term": [
            {
                "id": 4211,
                "place": "NEW YORK CITY" 
            },
            {
                "id": 2675,
                "place": "WASHINGTON (DC)"

            }
          ],
  "term": [
            {
                "id": 4211,
                "place": "NEW YORK CITY" 
            },
            {
                "id": 2675,
                "place": "WASHINGTON (DC)"

            }
]

}

My AS3 code looks like:

public function parseData(e:Event):void
  {
   var loader:URLLoader = URLLoader(e.target);
   var values:Object = JSON.decode(loader.data);
   var term:Array = values.term;
   var counter:Number = 0;

   for (var key:Object in term)
   {
    payload[counter] = [term[key].id, term[key].place];
    counter++;
   }

      dispatchEvent(new Event(Event.COMPLETE));
  }

I can get the data from the first array, but how would I structure my code so that I could iterate through 2 or more "term" arrays?

Thanks

+4  A: 

JSON (or any other ordered mapping type) cannot hold duplicate keys.

The solution would be to restructure the JSON to be like this:

{   
    "terms": [
        [
            {
                "id": 4211,
                "place": "NEW YORK CITY" 
            },
            {
                "id": 2675,
                "place": "WASHINGTON (DC)"

            }
        ],
        [
            {
                "id": 4211,
                "place": "NEW YORK CITY" 
            },
            {
                "id": 2675,
                "place": "WASHINGTON (DC)"

            }
        ]
    ]
}
LiraNuna
+2  A: 

One thing I noticed is that your JSON is slightly strange and causes your error. The main class of your JSON is a dictioniary defining the term twice. This does not cause an error but does cause the values.term to be overwritten the second time. You should change your JSON to something like:

{   
    "term": [[
            {
                "id": 4211,
                "place": "NEW YORK CITY" 
            },
            {
                "id": 2675,
                "place": "WASHINGTON (DC)"

            }],
            [{
                "id": 4211,
                "place": "NEW YORK CITY" 
            },
            {
                "id": 2675,
                "place": "WASHINGTON (DC)"

            }]
       ]

}

and your code to:

public function parseData(e:Event):void
  {
   var loader:URLLoader = URLLoader(e.target);
   var values:Object = JSON.decode(loader.data);
   var term:Array = values.term;
   var counter:Number = 0;

   for (var keys:Object in term)
   {
     for (var key:Object in term[keys])
     {
        payload[counter] = [term[keys][key].id, term[keys][key].place];
        counter++;
     }
   }

   dispatchEvent(new Event(Event.COMPLETE));
}
Vincent Osinga