How to decode and cast JSON string in Flex? How to decode and cast JSON string in Flex? json json

How to decode and cast JSON string in Flex?


You need to do something similar to what this page says: http://benrimbey.wordpress.com/2009/06/20/reflection-based-json-validation-with-vo-structs/

The problem with your code is you are trying to downcast a native Object into a specific Class instance that it knows nothing about. The structures of your two types are different. UserInfo inherits from Object (in a sort of funky AS3 way because of the way Classes are compiled), but b is a simple Object.


FYI if you are just doing JSON decoding, and it is a Flex app, not AIR. You do not need the as3Corelib package to do so. You can just use the parent browser's JavaScript interpreter like this:

var myJSONString:String = "{name:'Joe',age:35}";var myObj:Object = ExternalInterface.call('eval', "("+myJSONString+")");

This might save your user a few Kb on the download.


Glenn's link really did the trick. I also added a conversion between dot-net and AS3 - it seems that dot-net writes the __type attribute like so: "Class:Namespace", but AS3 needs it to be like so: "Namespace.Class".

private static function convertDotNetToASNameType(nameType:String):String            {    return(nameType.split(':').reverse().join('.'));}

BTW, if you are using Glenn's link and a WCF server, be sure to replace "clientClassPath" with dot-net's "__type".