sharmacal
sharmacal

Reputation: 457

Parsing JSON data in actionscript 3

I have a JSON response coming up as

[{"serial": "1", "name": "ABC" }]

I am using com.adobe.serialization.JSON class of as3corelib in actionscript. This class works perfectly in other formats of json, but in this case, after doing a JSON.decode to the object I am unable to get the value as obj.name

If I remove the square brackets I am getting a parsing error.

How can I parse the json in this case, I need to get the value of the key "name". Thanks.

Upvotes: 0

Views: 3012

Answers (2)

Subash Selvaraj
Subash Selvaraj

Reputation: 3385

You are parsing array of json objects. You need to fetch the value like obj[0].name.

Hope it helps.

Upvotes: 0

Khalil Bhm
Khalil Bhm

Reputation: 398

Technically to parse all the objects you'll need

// serverResponse answer from the server [String]
var jsonResponse:Object = JSON.parse(serverResponse);
for ( var object:Object in jsonResponse ){
   trace(object.name);
}

Well as you edited this is using : ActionScript 3 JSON and it's not just for air, you still can use it


And using AS3CoreLib, here is an example showing how to handle the JSON, so it look like this :

var rawData:String = String(event.result);
var arr:Array = (JSON.decode(rawData) as Array);
for (var i:int = 0; i < arr.length; i++){
    trace(arr[i].name);
}

Hope this helps, Cheers !!

Upvotes: 1

Related Questions