开发者

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

开发者 https://www.devze.com 2023-03-13 01:20 出处:网络
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?

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.


In flex 4.5 it become parse instead of decode

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


Try this also working

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


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

精彩评论

暂无评论...
验证码 换一张
取 消