thanks,
Drew
// var json contains valid JSON
var json:String = JSON.decode(json) as Array;
It always returns null.
I am looking for an alternate JSON decoder for Flash CS 3....
Thanks,
-Carlos-
var json:String = "[ 'hello', 'world' ]";
var results:Array = JSON.docde( json ) as Array;
trace( results[ 0 ] ); // hello
trace( results[ 1 ] ); // world
If you're still getting null results, I'd be interested to see what your
json string looks like before it's passed to the decoder.
-d
I've uploaded a test.zip file to this group here:
http://groups.google.com/group/as3corelib/files
Unzip it, and open test.as (Document class for test.fla). It simply
imports com.adobe.....JSON, and tries to use it as simply as possible.
The encode function seems to work, and it cannot even decode a string
it encoded.
Your example above doesn't even work, however it works rudimentarily
by switching your single for double quotes / vice versa. (The
commented out line in test.as.)
I greatly appreciate any help you can give my wrecked and tortured
soul.
Problem is resolved! Thank God. The problem was in Flex, I could cast
the return value of JSON.decode() to an Array. This wasn't working in
Flash, but I cast to Object and it seems to work.
Thanks,
-Carlos-
The result from JSON.decode can be either Object, Array, String, Number,
Boolean or null. If you don't know what the expected return type from
decode is going to be, you can simply say:
var result:* = JSON.decode( jsonString ); // could return any type
// Test to see what the return type is
if ( result is String )
{
// ...
}
else if ( result is Number )
{
// ...
}
// .. etc.
Additionally, the "it works by swapping single quotes with double
quotes" is as expected as well. The JSON decoder is pertty strict
strict -- i.e. it only accepts a 100% valid JSON string as input (and
it's not very forgiving). If you take a look at the spec on json.org, a
string needs to be wrapped in double quotes and not single quotes. My
apologies for typing incorrect code in an email. It's on my list to
make the decoder a little more forgiving, but I haven't had the time to
devote to it lately.
I'm glad you got it working after all.
-d