Jacek
Jacek

Reputation: 1914

Parsing JSON in ActionScript (Adobe Flex). JSON.decode(string) returns null

I am trying to parse JSON file in my Flex project. I included as3corelib.swc and imported com.adobe.serialization.json.JSON, but JSON.decode() function still returns null. What might be the problem?

[Embed(source="assets/test.json",mimeType="application/octet-stream")]
private var json_file:Class;

public function load():void
{
    var bytes:ByteArray = new json_file();
    var json:String = bytes.readUTFBytes(bytes.length);
    trace(json); // String is OK!
    var arr:Array = (JSON.decode(json) as Array);
    trace(arr); // Array is null!
}

I also tried:

    var str:String = (JSON.decode(json) as String);
    trace(arr); // null!

and:

    var arr:Object = JSON.decode(json); // [object Object]
    trace(arr.toString()); // empty string

Thanks for your time.

Upvotes: 1

Views: 12486

Answers (3)

Thirumalai murugan
Thirumalai murugan

Reputation: 5896

In flex 4.5 it become parse instead of decode

var obj:Object=JSON.parse(json);

Upvotes: 3

Dinesh
Dinesh

Reputation: 566

Try this also working

var arr:Array = (JSON.decode(json) as Array);
for (var keyname:String in arr)
{
trace ( keyname + ": " + arr[ keyname ] );          
}   

Upvotes: 2

Jacek
Jacek

Reputation: 1914

Problem solved thanks to J_A_X (see comments to the question). Elements can be accessed by key. Example:

var obj:Object = JSON.decode(json);
trace(obj.GlossEntry[0].Acronym.toString());

Upvotes: 1

Related Questions