AS3CoreLib JSON serialization
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
-
相關文章
- 高效的 Json 解析框架 kotlinx.serializationJSON框架Kotlin
- [.net 物件導向程式設計進階] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json類物件程式設計JSON
- 物件的序列化(Serialization)物件
- kotlinx.serialization 1.0正式版Kotlin
- LintCode-Serialization and Deserialization Of Binary Tree
- java安全編碼指南之:序列化SerializationJava
- LeetCode-Verify Preorder Serialization of a Binary TreeLeetCode
- Serialize Your Deck with Positron [XML Serialization, XSD, C#]XMLC#
- [.net 物件導向程式設計進階] (12) 序列化(Serialization)(四) 快速掌握JSON的序列化和反序列化物件程式設計JSON
- 使用 deploy 部署專案時報 Serialization of 'Closure' is not allowed 錯誤
- 【LeetCode】Verify Preorder Serialization of a Binary Tree(331)LeetCode
- django rest framework個人學習筆記(三)————Tutorial1.SerializationDjangoRESTFramework筆記
- 關於System.Web.Script.Serialization名稱空間的引用Web
- 【json】json基礎知識JSON
- 《JSON》JSON
- JSONJSON
- Json hijacking/Json劫持漏洞JSON
- 高效生成JSON串——json-genJSON
- json例項練習 json物件JSON物件
- Json物件與Json字串互轉JSON物件字串
- json轉json樹狀結構JSON
- 比 encoding/json 更快地解析 jsonEncodingJSON
- JSON.parse(str),JSON.stringify(a)JSON
- js把json字串轉成json物件JSON字串物件
- JSON.parse()和JSON.stringify()JSON
- JSON及Python操作JSON相關JSONPython
- json 物件與json 字串的區別。JSON物件字串
- JSON 物件JSON物件
- json模組JSON
- 拼JSONJSON
- JSON 格式JSON
- JSON格式JSON
- JavaScript JSONJavaScriptJSON
- JSON 初探JSON
- JSON列印JSON
- 聊聊jsonJSON
- java jsonJavaJSON
- lazarus jsonJSON