AS3CoreLib JSON serialization
- Author: 柳大·Poechant(钟超)
- Email: zhongchao.ustc#gmail.com (# -> @)
- Blog:Blog.CSDN.net/Poechant
- Date: May 2nd, 2012
Example #1: Directly generate JSON object
How to generate a JSON object directly?
package {
import com.adobe.serialization.json.JSON;
import flash.display.Sprite;
public class test3 extends Sprite {
public function test3() {
var str:String = "{\"nations\": " +
"[{\"country\":\"P.R.China\", \"capital\":\"Beijing\"}, " +
"{\"country\":\"U.S.\", \"capital\":\"Washington D.C.\"}]" +
"}";
var json:Object = new Object();
json = JSON.decode(str);
trace(json.nations[0].country);
}
}
}
Output:
P.R.China
Example #2: Serialize an object to JSON
You can serialize an object containing primary type members to a JSON object. RememberPRIMARY.
package {
import com.adobe.serialization.json.JSON;
import flash.display.Sprite;
import flash.utils.ByteArray;
public class test3 extends Sprite {
public function test3() {
var example:Example = new Example();
example._int = 3;
example._number = 4.5;
example._string = "hello";
example._boolean = true;
var json:String = JSON.encode(example);
trace(json);
}
}
}
import flash.utils.ByteArray;
class Example {
public var _int:int;
public var _number:Number;
public var _string:String;
public var _boolean:Boolean;
public function Example() {
}
}
output:
{"_number":4.5,"_boolean":true,"_int":3,"_string":"hello"}
Can a ByteArray be serialized to a JSON?
Yes, but…
package {
import com.adobe.serialization.json.JSON;
import flash.display.Sprite;
import flash.utils.ByteArray;
public class test3 extends Sprite {
public function test3() {
var example:Example = new Example();
example._int = 3;
example._number = 4.5;
example._string = "hello";
example._boolean = true;
example._byteArray.writeInt(1234);
var json:String = JSON.encode(example);
trace(json);
var result:Object = new Object();
result = JSON.decode(json);
trace(result._byteArray);
}
}
}
import flash.utils.ByteArray;
class Example {
public var _int:int;
public var _number:Number;
public var _string:String;
public var _boolean:Boolean;
public var _byteArray:ByteArray;
public function Example() {
_byteArray = new ByteArray();
}
}
You will see the output:
{"_byteArray":{"length":4,"position":4,"bytesAvailable":0,"objectEncoding":3,"endian":"bigEndian"},"_number":4.5,"_boolean":true,"_int":3,"_string":"hello"}
[object Object]
Yes, only those member of primary types are serialized into JSON result…So CAUTION! When you wanna use JSON, just keep your eyes on PRIMARY types.
Reference
- JSON - ActionScript® 3.0 Reference for the Adobe® Flash® Platform
- Github - mikechambers/as3corelib
- JSON扫盲帖+JSON类教程
-
转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant
-
分享到:
相关推荐
flex4.5利用as3corelib.swc解析json实例, 内含as3corelib.swc
as3corelib-.93,as3corelib-.92, as3corelib-.92.1,corelib-.90
adobe官方类库,包括json解析类,md5加密类等
as3corelib的CHM API帮助文档
as3corelib-.93.zip as3corelib-.93.zip
as3一牛逼类库as3corelibas3corelibas3corelibas3corelib
as3corelib github 源码,json解析例子和相关deom.从github 上下载,用于学习和代码交流
as3corelib是一个开源的as库,里面有很多有用的工具类,其中里面的JSON类可以将AS的对象转换成JSON的格式。
com.adobe.serialization.json.JSON; flex使用json报错,没有定义的类型,导入包即可. 资源是rar文件, 里面有源码+示例 和一个as3corelib.swc文件
as3 官方推荐的扩展核心库 actionscript3语言的,包含图片处理,数组运算,网络连接等扩展包
最新编译的AS3CORELIB,执行把此文件解压,然后拷贝到sdk,frameworks,libs目录下,即可使用。
NULL 博文链接:https://as3.iteye.com/blog/1003737
NULL 博文链接:https://kenkao.iteye.com/blog/1441705
as3corelib.swc FLEX第三方组件包 as3corelib.swc as3corelib.swc FLEX第三方组件包 as3corelib.swc
包含大部分as3需要用的解码