I'm using as3corelib to decode/encode JSON strings. In my little experiment I want to encode
an object (UserInfo) to JSON string and decode it back to the object, however it seems to fail at the convertion point (the last line), why would that happen? how can I make it work?
UserInfo classpublic class UserInfo
{
public var levelProgress : int;
}
var user1:UserInfo = new UserInfo()
user1.levelProgress = 20;
var a:String = JSON.encode(user1);
var b:Object = JSON.decode(a);
var c:UserInfo;
c = b as UserInfo; // c gets null, why?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With