JSON literacy post JSON. as tutorial _ json

Source: Internet
Author: User
If the json string uses FlashVars to pass Parameters from html, the content after the first double quotation mark (including double quotation marks) in the string will not be transmitted. Additional content:
If the json string uses FlashVars to pass Parameters from html, the content after the first double quotation mark (including double quotation marks) in the string will not be transmitted. adobe's official json. parsing josn objects in the as class contains double quotation marks. so after half a day, I replaced the function with a string!
Method to read another article:
How to transmit parameters with double quotation marks in html to flash
Bytes --------------------------------------------------------------------------------------
Let's take a look at the reposted content below! The preceding content is based on your own needs:
Bytes --------------------------------------------------------------------------------------
Yesterday, I posted a json AS3 parsing class. From the posts we posted, many people still don't know about this. I have created a special literacy post.
In fact, using json in AS is not a must or a good choice, because AS is already a good choice for xml parsing, but why can we consider using json? There are the following points:
Json is between plain text (such as: firstName = Brett & lastName = McLaughlin & email = brett@newInstance.com) and xml ( Brett McLaughlin Brett@newInstance.com A format in the middle, which has the neutral advantage of text and xml: small data size and clear data format.
Json is short for JavaScript Object Notation, which means it comes from javascript. Due to the popularity of ajax, most websites adopt the ajax mode and architecture, json will be the first choice for data transmission (the text method is too simple to understand when there is a large amount of data, the xml method has a large amount of data, which will increase the server load during parsing ), if a website uses json to generate a flex/flash Interface Based on the ajax architecture, the server-side program changes will be minimized.
The server now has mature JSON Parsing Code (because JSON is widely used), so you don't have to worry about server-side parsing during development.
Ps: How can I only think of the above three points? Will there be too few reasons?
What is JSON?
Simple JSON example
In the simplest form, you can use the following JSON to represent name/value pairs:

The Code is as follows:


{"FirstName": "Brett "}


This example is very basic and actually takes more space than the equivalent plain text name/Value Pair:

The Code is as follows:


FirstName = Brett


However, when multiple name/value pairs are concatenated, JSON will reflect its value. First, you can create records that contain multiple name/value pairs, for example:

The Code is as follows:


{"FirstName": "Brett", "lastName": "McLaughlin", "email": "brett@newInstance.com "}


In terms of syntax, this is not a great advantage over name/value pairs, but JSON is easier to use and more readable in this case. For example, it explicitly indicates that the above three values are part of the same record; curly braces make these values have a certain relationship.
Array of Values
To represent a group of values, JSON not only improves readability, but also reduces complexity. For example, assume that you want to list a person's name. In XML, many start and end tags are required. If a typical name/value pair is used (like the name/value pair seen in the previous articles in this series ), you must either create a proprietary data format or change the key name to a person1-firstName.
If JSON is used, you only need to group multiple records with curly braces:

The Code is as follows:


{"People ":[
{"FirstName": "Brett", "lastName": "McLaughlin", "email": "brett@newInstance.com "},
{"FirstName": "Jason", "lastName": "Hunter", "email": "jason@servlets.com "},
{"FirstName": "Elliotte", "lastName": "Harold", "email": "elharo@macfaq.com "}
]}


This is not hard to understand. In this example, there is only one variable named people. The value is an array containing three entries, each of which is a one-person record, including the name, last name, and email address. The preceding example demonstrates how to use parentheses to combine records into a value. Of course, you can use the same syntax to represent multiple values (each value contains multiple records): {"programmers ":[

The Code is as follows:


{"FirstName": "Brett", "lastName": "McLaughlin", "email": "brett@newInstance.com "},
{"FirstName": "Jason", "lastName": "Hunter", "email": "jason@servlets.com "},
{"FirstName": "Elliotte", "lastName": "Harold", "email": "elharo@macfaq.com "}
],
"Authors ":[
{"FirstName": "Isaac", "lastName": "Asimov", "genre": "science fiction "},
{"FirstName": "Tad", "lastName": "Williams", "genre": "fantasy "},
{"FirstName": "Frank", "lastName": "Peretti", "genre": "christian fiction "}
],
"Musicians ":[
{"FirstName": "Eric", "lastName": "Clapton", "instrument": "guitar "},
{"FirstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano "}
]
}


The most noteworthy here is that it can represent multiple values, and each value contains multiple values. However, you should also note that the actual name/value pairs in the record can be different between different primary entries (programmers, authors, and musicians. JSON is completely dynamic and allows you to change the data representation mode in the middle of the JSON structure.
When processing data in JSON format, there are no predefined constraints to be observed. Therefore, in the same data structure, you can change the way data is represented, or even express the same thing in different ways.
Ps: The above examples are from http://www.ibm.com/?works/cn/web/wa-ajaxintro10/. you can master Ajax and be lazy.

When Dangdang came back, I went back to work last week without worrying about the usage of the json class. Next I will introduce the usage of adobe's json class.
The last post of the json class has a problem (http://bbs.actionscript3.cn/thread-1625-1-1.html) Because I also downloaded it from someone else who knows it's a semi-finished product. Don't be angry. We suggest the Administrator delete it!
This is adobe's official class. I modified the package and I can use it this time.
The following is a simple tutorial:
1. server-side json
I won't talk about how to get the server-side json (Communication), so it should be a string that is saved to the serverJSON variable. The usage is as follows:

The Code is as follows:


Import json .*;
Var json: Object = new Object ();
Json = JSON. decode (serverJSON );



Json is an object.
For example:
The above JSON code is as follows:

The Code is as follows:


{"Programmers ":[
{"FirstName": "Brett", "lastName": "McLaughlin", "email": "brett@newInstance.com "},
{"FirstName": "Jason", "lastName": "Hunter", "email": "jason@servlets.com "},
{"FirstName": "Elliotte", "lastName": "Harold", "email": "elharo@macfaq.com "}
],
"Authors ":[
{"FirstName": "Isaac", "lastName": "Asimov", "genre": "science fiction "},
{"FirstName": "Tad", "lastName": "Williams", "genre": "fantasy "},
{"FirstName": "Frank", "lastName": "Peretti", "genre": "christian fiction "}
],
"Musicians ":[
{"FirstName": "Eric", "lastName": "Clapton", "instrument": "guitar "},
{"FirstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano "}
]
}


Saved variable: serverJSON
Code:

The Code is as follows:


Var serverJSON: String = '{"programmers": [{"firstName": "Brett", "lastName": "McLaughlin", "email": "brett@newInstance.com "}, {"firstName": "Jason", "lastName": "Hunter", "email": "jason@servlets.com" },{ "firstName": "Elliotte", "lastName ": "Harold", "email": "elharo@macfaq.com"}], "authors": [{"firstName": "Isaac", "lastName": "Asimov", "genre ": "science fiction" },{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },{ "firstName ": "Frank", "lastName": "Peretti", "genre": "christian fiction"}], "musicians": [{"firstName": "Eric ", "lastName": "Clapton", "instrument": "guitar" },{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument ": "piano"}]}'
Var s: Object = JSON. decode (serverJSON );
// Start to use
Trace (s. programmers [0]. firstName); // output: Brett



It's not that simple. In fact, it becomes an object after the transformation. You can access these values through the dot syntax. XML goes by the edge.
2. Make the local object JSON
If you can spell out JSON strings by yourself, but we are in the object-oriented world, so we are all objects, and then the objects can be used directly.
For example:

The Code is as follows:


Import json .*;
Var myObject: Object = new Object ();
MyObject. AB = "adfsdf ";
MyObject. cd = Math. random ();
Trace (JSON. encode (myObject); // output: {"AB": "adfsdf", "cd": 0.0599129400216043}


In this way, you can give it to the server.
Summary: There are two methods, JSON. decode (String), JSON. encode (Object), which is a simple method to achieve a small amount of transmission, and a simple data format, why do we not need it?
In fact, XML naturally has its own strength. When a complicated data structure appears, JSON is hard to solve at this time, and XML is the first choice.

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.