JSON Literacy post Json.as class tutorial _json

Source: Internet
Author: User
Tags advantage

Supplementary content:
If the JSON string is passed an argument from HTML with Flashvars, then the contents of the first double quotation mark (including double quotes) in the string are not transferred. Adobe's official json.as class bag resolves josn objects with double quotes. So we've been doing this for half a day and then replacing the function with a string!
method to see another article:
HTML with double quote parameters for Flash Solutions
--------------------------------------------------------------------------------------
First look at the following reproduced content it! These are supplemented by your own needs:
--------------------------------------------------------------------------------------
Yesterday sent a JSON AS3 parsing class, from the point of view of everyone, many people still do not know this thing, special build a literacy stickers.
In fact, using JSON in as is not a must or a good choice, because as XML parsing is pretty good, but why is it possible to consider using JSON, with the following points:
JSON is a simple textual approach (such as: firstname=brett&lastname=mclaughlin&email=brett@newinstance.com) and XML (<request> <firstName>Brett</firstName><lastName>McLaughlin</lastName><email> brett@newinstance.com</email></request>) in the middle of a format, he has the neutral advantage of text and XML: The data volume is small and clear data format.
JSON is shorthand for JavaScript Object notation, which means that he is something that comes from JavaScript. Because of the current popularity of Ajax, most of the Web site will adopt the AJAX model and architecture, then JSON will be a data transfer of choice (text is too simple, if the large amount of data can not understand the size of the XML, the way the data volume, in the resolution will increase the server burden), If a Web site uses JSON to create a flex/flash version of the interface from an AJAX architecture, it will minimize server-side program changes.
The server side now has mature JSON parsing code (because JSON is too widely used), so there is no need to worry about server-side parsing when developing.
PS: How can I only think of the above three points ah, will not reason too little?
What is JSON?
Simple JSON Example
In the simplest form, you can represent name/value pairs with JSON such as the following:

Copy Code code as follows:

{"FirstName": "Brett"}

The example of copying code is very basic and actually takes up more space than an equivalent plain text name/value pair:
Copy Code code as follows:

Firstname=brett

Copy code However, when multiple name/value pairs are strung together, JSON reflects its value. First, you can create records that contain multiple name/value pairs, such as:
Copy Code code as follows:

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

Copying code from a syntactic standpoint, this has no significant advantage over name/value pairs, but in this case JSON is easier to use and more readable. For example, it is clear that the above three values are part of the same record; curly braces make these values a bit of a connection.
Array of values
When you need to represent a set of values, JSON not only improves readability, but it also reduces complexity. For example, suppose you want to represent a list of people. In XML, many start and end tags are required, and if you use a typical name/value pair (like the name/value pairs that you see in previous articles in this series), you must either create a proprietary data format or modify the key name to form Person1-firstname.
If you use JSON, you only need to group together multiple records with curly braces:
Copy Code code 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"}
]}

Copy code It's not hard to understand. In this example, there is only one variable named people, and the value is an array of three entries, each of which is a record of one person, including first, last name, and e-mail address. The preceding example shows how to combine records into a single value in parentheses. Of course, you can use the same syntax to represent multiple values (each value contains multiple records): {"Programmers": [
Copy Code code 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"}
]
}

Copying code The most notable thing here is that you can represent multiple values, each of which in turn contains multiple values. It should also be noted, however, that the actual name/value pairs in the record can be different between the main entries (programmers, authors, and musicians). JSON is completely dynamic, allowing you to change the way the data is represented in the middle of the JSON structure.
There are no predefined constraints to follow when working with JSON-formatted data. So, in the same data structure, you can change the way that you represent the data, and you can even represent the same thing in different ways.
PS: The above examples are from http://www.ibm.com/developerworks/cn/web/wa-ajaxintro10/, Master Ajax, their lazy, look at others have on the way

When I came back, I was busy doing this work last week, and here's how to use Adobe's JSON class.
There was a problem with the last JSON class (http://bbs.actionscript3.cn/thread-1625-1-1.html) because I downloaded it from someone else, who knew it was a semi-finished product. Hope everyone do not angry Ah, advised the administrator to delete!
This is the official Adobe class, I modified the package, this time it can be used.
The following is a tutorial that is relatively simple:
1, server-side JSON
How to get server-side JSON I'm not going to say it. (that is, communication), then the resulting should be a string, stored in variable Serverjson, use the following way:

Copy Code code as follows:

Import json.*;
var json:object = new Object ();
JSON = Json.decode (Serverjson);


JSON is an object, it's easy.
Give an example:
A section of the JSON code above:
Copy Code code 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"}
]
}

Deposit Variable: Serverjson
Code:

Copy Code code 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", "InStr Ument ":" Guitar "},{" FirstName ":" Sergei "," LastName ":" Rachmaninoff "," Instrument ":" Piano "}]} '
var s:object = Json.decode (Serverjson);
Start using
Trace (s.programmers[0].firstname);//output: Brett


No, it's so simple. In fact, after the transformation becomes an object, you can access these values through the point syntax. XML pull over.
2, local objects make JSON
If you can spell out the JSON string, but we are in the object-oriented world Ah, then we are objects ah, then the object can be used directly.
Give an example:

Copy Code code 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}

This will be given to the server.
Summary: In two ways, Json.decode (String), Json.encode (Object), there is such a simple way to achieve a small amount of transmission, and simple data format, why do we not use it?
In fact, XML naturally has its own strength, when a complex structure of data structure appears, this time JSON is difficult to fix, 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.