actionScript 解析json开源包,比官方的好

前段时间参与了一个移动的孵化器项目,用flex来解析json,本来想用官方的as3corelib来解析json文件,不过我发现他生成的json没有格式化,生成的一堆字符串混在一起,看这不舒服,我想其他人用这个包的时候也会和我有同样的感觉,我的想法是正确的,确实有人重写了官方的json包(adobe开源的东东就是不怎么样),试了一下,解析后,果然很有条理。。。

Json代码   收藏代码
  1. {  
  2.     "id":"1010",  
  3.     "name":"公共素材库",  
  4.     "subclassifications":[  
  5.         {  
  6.             "id":"1011",  
  7.             "name":"悬疑"  
  8.         },  
  9.         {  
  10.             "id":"1012",  
  11.             "name":"搞笑"  
  12.         },  
  13.         {  
  14.             "id":"1013",  
  15.             "name":"生活"  
  16.         }  
  17.     ]  
  18. }  

  该包的JSON.as中的方法:

 

Actionscript代码   收藏代码
  1. /**  
  2.    * Encodes a object into a JSON string.  
  3.    *  
  4.    * @param o The object to create a JSON string for  
  5.    * @return the JSON string representing o  
  6.    * @langversion ActionScript 3.0  
  7.    * @playerversion Flash 9.0  
  8.    * @tiptext  
  9.    */  
  10.   public static function encode( o:Object, pretty:Boolean=false, maxLength:int=60 ):String  
  11.   {   
  12.    return new JSONEncoder( o, pretty, maxLength ).getString();  
  13.   }  
Actionscript代码   收藏代码
  1. /**  
  2.    * Decodes a JSON string into a native object.  
  3.    *   
  4.    * @param s The JSON string representing the object  
  5.    * @param strict Flag indicating if the decoder should strictly adhere  
  6.    *   to the JSON standard or not.  The default of <code>true</code>  
  7.    *   throws errors if the format does not match the JSON syntax exactly.  
  8.    *   Pass <code>false</code> to allow for non-properly-formatted JSON  
  9.    *   strings to be decoded with more leniancy.  
  10.    * @return A native object as specified by s  
  11.    * @throw JSONParseError  
  12.    * @langversion ActionScript 3.0  
  13.    * @playerversion Flash 9.0  
  14.    * @tiptext  
  15.    */  
  16.   public static function decode( s:String, strict:Boolean = true ):*  
  17.   {   
  18.    return new JSONDecoder( s, strict ).getValue();   
  19.   }  

 官方的我就不贴上来了,有兴趣的童鞋可以自己去官网看。。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值