前段时间参与了一个移动的孵化器项目,用flex来解析json,本来想用官方的as3corelib来解析json文件,不过我发现他生成的json没有格式化,生成的一堆字符串混在一起,看这不舒服,我想其他人用这个包的时候也会和我有同样的感觉,我的想法是正确的,确实有人重写了官方的json包(adobe开源的东东就是不怎么样),试了一下,解析后,果然很有条理。。。
{ "id":"1010", "name":"公共素材库", "subclassifications":[ { "id":"1011", "name":"悬疑" }, { "id":"1012", "name":"搞笑" }, { "id":"1013", "name":"生活" } ] }
该包的JSON.as中的方法:
/** * Encodes a object into a JSON string. * * @param o The object to create a JSON string for * @return the JSON string representing o * @langversion ActionScript 3.0 * @playerversion Flash 9.0 * @tiptext */ public static function encode( o:Object, pretty:Boolean=false, maxLength:int=60 ):String { return new JSONEncoder( o, pretty, maxLength ).getString(); }
/** * Decodes a JSON string into a native object. * * @param s The JSON string representing the object * @param strict Flag indicating if the decoder should strictly adhere * to the JSON standard or not. The default of <code>true</code> * throws errors if the format does not match the JSON syntax exactly. * Pass <code>false</code> to allow for non-properly-formatted JSON * strings to be decoded with more leniancy. * @return A native object as specified by s * @throw JSONParseError * @langversion ActionScript 3.0 * @playerversion Flash 9.0 * @tiptext */ public static function decode( s:String, strict:Boolean = true ):* { return new JSONDecoder( s, strict ).getValue(); }
官方的我就不贴上来了,有兴趣的童鞋可以自己去官网看。。。。