To use JSON, read an article (http://bbs.actionscript3.cn/thread-1657-1-1.html) Literacy stickers, the use of JSON In Flex is very convenient. JSON is a format between plain text and XML. What JSON can do is to complete XML. Why should I use JSON? I think most of them are the same as me and have to be used. JSON is the first choice for Ajax data transmission. The existing project uses JSON. If the flex interface is added with JSON, there will be very few changes. I think the three reasons mentioned in the literacy post are the most adequate.
To use JSON In Flex, you must first use the next JSON package. It is official, but it cannot be carried out officially. It took 3 minutes to get down on csdn. In the attachment, you can use it as needed.
Paste an example:
<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute"
Creationcomplete = "Init ()">
<Mx: SCRIPT>
<! [CDATA [
Import com. Adobe. serialization. JSON .*;
Private var jsonstr: string;
Private var jsonobj: object;
Private var jsonobj2: object;
Private var jsonstr2: string;
Internal function Init (): void {
Jsonstr = '{"name": "zhanzhihu", "Age": 22, "gender": "male "}';
Jsonobj = new object ();
Jsonobj = JSON. Decode (jsonstr );
Trace (jsonobj. Name );
Jsonobj2 = new object ();
Jsonobj2.firstname = "bill ";
Jsonobj2.lastname = "Gate ";
Jsonobj2.com = "Microsoft ";
Jsonstr2 = JSON. encode (jsonobj2 );
Trace (jsonstr2 );
}
]>
</MX: SCRIPT>
</MX: Application>
Output:
Zhanzhihu
{"Firstname": "bill", "lastname": "Gate", "com": "Microsoft "}
Jsonstr is a string in JSON format. decode (string) can be parsed as an as object, and JSON is also used. encode (object) can convert the as object into a JSON string, which is convenient for the front-end flex to perform JSON operations.
Corelib: http://code.google.com/p/as3corelib/