Use JSON In Flex (repost to favorites)

Source: Internet
Author: User

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.