Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 Basic Parsing JSON

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

like image 620
minimalpop Avatar asked Jun 16 '26 09:06

minimalpop


2 Answers

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)"

            }
        ]
    ]
}
like image 180
LiraNuna Avatar answered Jun 19 '26 02:06

LiraNuna


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));
}
like image 33
Vincent Osinga Avatar answered Jun 19 '26 04:06

Vincent Osinga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!