8

I have a bit of a dilemma. I have a JSON object that has a format I'm unfamiliar with (starts with an array [] instead of an object {}) and was wondering how I might parse it in AS3. The object looks like:

[
    {
        "food": [
            {
                "name": "pasta",
                "price": 14.50,
                "quantity": 20
            },
            {
                "name": "soup",
                "price": 6.50,
                "quantity": 4
            }
        ]
    },
    {
        "food": [
            {
                "name": "salad",
                "price": 2.50,
                "quantity": 3
            },
            {
                "name": "pizza",
                "price": 4.50,
                "quantity": 2
            }
        ]
    }
]

I don't really know how I get to each food array, and each object within it. Any help would be greatly appreciated! Thanks!

Xavi
  • 20,111
  • 14
  • 72
  • 63
dtrainer45
  • 155
  • 2
  • 3
  • 5

4 Answers4

24

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

PiotrkS
  • 381
  • 3
  • 6
12

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); // Pizza
trace(food.price); // 4.50
trace(food.quantity); // 2
food.number++;
var newData:String = String(food);
trace(newData); // {"name":"pizza","price":"4.50","quantity":"2"}
Mat
  • 202,337
  • 40
  • 393
  • 406
medoix
  • 1,189
  • 2
  • 16
  • 36
  • i've tried using that method, however with the json i posted above, do i need to start with a JSONArray first, and then a JSON object? since it is wrapped first by [] and then by {}. – dtrainer45 Nov 11 '09 at 07:19
  • 2
    +1 AS3corelib is good. @dtrainer45: if you use adobe flexbuilder, add a breakpoint after deserializing the json string. Then you can explore the structure of the created object graph. If it is an array, it should probably be something like var x:JSONObject = new JSONObject(data); trace(x[0].food[0].name); not tested though. – Max Nov 11 '09 at 08:35
7

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);
  }
}
Xavi
  • 20,111
  • 14
  • 72
  • 63
Les
  • 2,316
  • 16
  • 17
2

I was looking for an alternative to a library and found the technique here. I'm assuming this will work in the context of the op (which was answered years ago of course) since it doesn't require a return type of Object. This works well for what I was trying to do when I found this post and I found the solution pretty elegant for flash based in the browser.

function json_decode( data:String ):* {
  try {
    return ExternalInterface.call("function(){return " + data + "}");
  } catch (e:Error) {
    return null;
  }
}
Shane
  • 4,921
  • 5
  • 37
  • 53
  • 1
    **Note:** As is, this a big security vulnerability. It would be better to use: `ExternalInterface.call("function(){return JSON.decode('" + data.replace(/(['\\])/g, "\\$1"); + "');}")` – brianreavis Apr 15 '13 at 05:51
  • 1
    @brianreavis: just `ExternalInterface.call('JSON.parse', data)` should do it :-) – Andy E Jan 02 '14 at 14:28
  • noted, thanks! At the time I just needed to get something working and I was in control of the data source. As an aside, this will not work with IE7 and lower due to lack of a native JSON api as per this question: http://stackoverflow.com/questions/4908875/is-json-parse-supported-by-all-major-browsers – Shane Jan 02 '14 at 16:32