AS3 JSON parsing AS3 JSON parsing json json

AS3 JSON parsing


from flash player 11, and sdk 4.6 there is native support for json. To use it you should change

var foods:Array = JSON.decode(jsonstring);

to

var foods:Array = JSON.parse(jsonstring);

while JSON is not from as3corelib but from sdk itself. Pretty much faster ;)


You will need to use the JSON Object Class (below link)http://code.google.com/p/as3corelib/

and then something like this..

var data:String = "{\"name\":\"pizza\",\"price\":\"4.50\",\"quantity\":\"2\"}";var food:JSONObject = new JSONObject(data);trace(food.name); // Pizzatrace(food.price); // 4.50trace(food.quantity); // 2food.number++;var newData:String = String(food);trace(newData); // {"name":"pizza","price":"4.50","quantity":"2"}


Interesting datastructure... this should do it:

import com.adobe.serialization.json.JSON;/* ... other code ... */var foods:Array = JSON.decode(jsonstring);for(var i:int = 0; i < foods.length; i++) {  for(var j:int = 0; j < foods[i].length; j++) {    trace(foods[i][j].name);  }}