1

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.

3
  • 1
    Could be either bad JSON, or you aren't using it properly. JSON uses key-value pairs and is always decoded into an object which has properties. You should access the decoded object as Object.someKey to get the data. Commented Jun 15, 2011 at 17:45
  • JSON is good. I checked it with a validator. Commented Jun 15, 2011 at 17:55
  • You are right J_A_X. I can access elements with: trace(arr.GlossEntry[0].Acronym.toString()); Commented Jun 15, 2011 at 17:58

3 Answers 3

3

In flex 4.5 it become parse instead of decode

var obj:Object=JSON.parse(json);
Sign up to request clarification or add additional context in comments.

Comments

2

Try this also working

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.