Home >Web Front-end >JS Tutorial >JSON literacy post JSON.as class tutorial_json

JSON literacy post JSON.as class tutorial_json

WBOY
WBOYOriginal
2016-05-16 18:56:04926browse

Supplementary content:
If the json string is passed from HTML using FlashVars, the content after the first double quotation mark (including double quotation marks) in the string will not be transmitted. And Adobe's official Double quotes are indispensable when parsing josn objects in the json.as class package. So after working on it for a long time, I just used a string replacement function!
See another article for the method:
HTML passing parameters with double quotes Give flash solution
--------------------------------------------- ---------------------------------------------
Look at the following first Reprint the content! The previous content is supplementary content based on your own needs:
-------------------------------- -------------------------------------------------- ----
Yesterday, I posted the AS3 parsing class for json. Judging from everyone’s comments, many people still don’t know about this thing, so I created a literacy post.
In fact, using json in AS is not a must or a good choice, because AS’s parsing of xml is already very good, but why should you consider using json? There are the following points:
json is Between the simple text method (such as: firstName=Brett&lastName=McLaughlin&email=brett@newInstance.com) and xml (BrettMcLaughlin brett@newInstance.com) is a format in the middle. It has the neutral advantages of text and xml: small data volume and clear data format.
json is the abbreviation of JavaScript Object Notation, which means that it comes from JavaScript. Because of the popularity of ajax now, most websites will adopt the ajax mode and structure, so json will be the first choice for data transmission (the text method is too simple and cannot be understood if there is a large amount of data. The xml method has a large amount of data and needs to be parsed. will increase the load on the server), then if a website develops a flex/flash version of the interface based on the ajax architecture, using json will minimize the server-side program changes.
The server side now has mature JSON parsing code (because JSON is so widely used), so you don’t have to worry about server-side parsing during development.
ps: Why can I only think of the above three points? Are there too few reasons?
What is JSON?
Simple JSON example
In its simplest form, the following JSON can be used to represent name/value pairs:

Copy Code The code is as follows:

{ "firstName": "Brett" }

Copy Code This example is very basic and actually Takes up more space than the equivalent plain text name/value pair:
Copy code The code looks like this:

firstName=Brett

Copy code However, JSON comes into its own when you string multiple name/value pairs together. First, you can create a record containing multiple name/value pairs, such as:
Copy code The code is as follows:

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

Copy the code from a syntax perspective, This isn't a huge advantage over name/value pairs, but JSON is easier to use and more readable in this case. For example, it makes it clear that the above three values ​​are part of the same record; the curly braces make the values ​​somehow related.
Array of values ​​
When you need to represent a set of values, JSON can not only improve readability, but also reduce complexity. For example, suppose you want to represent a list of people's names. In XML, many start and end tags are required; if you use typical name/value pairs (like the ones you saw in previous articles in this series), a proprietary data format must be created , or change the key name to the form person1-firstName.
If using JSON, just group multiple records with curly braces together:
Copy code 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" }
]}

Copy the code This is not difficult to understand. In this example, there is only one variable called people, and the value is an array of three entries, each entry being a record for a person containing a first name, last name, and email address. The above example demonstrates how to use parentheses to combine records into a single value. Of course, you can use the same syntax to represent multiple values ​​(each containing multiple records): { "programmers": [
Copy code 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" }
]
}

Copy code The most noteworthy thing here is the ability to represent multiple values, each of which in turn contains multiple values. However, it should also be noted that the actual name/value pairs in the record can differ between different main entries (programmers, authors, and musicians). JSON is fully dynamic, allowing the way data is represented to change in the middle of the JSON structure.
There are no predefined constraints that need to be adhered to when processing JSON formatted data. Therefore, within the same data structure, the way the data is represented can be changed, and the same thing can even be represented in different ways.
ps: The above examples are all from http://www.ibm.com/developerworks/cn/web/wa-ajaxintro10/. I master Ajax. I am lazy, so I just use it when others have it.

Dangdang, I'm back again. I was busy at work last week and didn't bother to write down the usage. Here I will introduce the usage of Adobe's json class.
There is a problem with the json class published last time (http://bbs.actionscript3.cn/thread-1625-1-1.html), because I also downloaded it from someone else, who knew it was a semi-finished product. I hope everyone won't be angry. I suggest the administrator delete it!
This time it is Adobe’s official class. I modified the package and it can be used this time.
The following is a tutorial, which is relatively simple:
1. JSON from the server
I won’t go into details about how to get the JSON from the server (that is, communication), then what you get should be a string, save Enter the variable serverJSON and use it as follows:

Copy the code The code is as follows:

import json.*;
var json:Object = new Object();
json = JSON.decode(serverJSON);


json is an object, simple.
Give an example:
A piece of code from the above JSON:
Copy the code 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" }
]
}

Save to variable :serverJSON
Code:

Copy 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 using
trace(s.programmers[0].firstName);//Output: Brett


It’s not that simple. In fact, it becomes an object after conversion, and these values ​​can be accessed through dot syntax. XML aside.
2. Local objects are made into JSON
You can also spell out the JSON string yourself, but we are in an object-oriented world, so we are all objects. When the time comes The object can be used directly.
Give an example:

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

This way it can be given to the server.
Summary: There are only two methods, JSON.decode(String) and JSON.encode(Object). There is such a simple way to achieve small transmission volume and simple data format. Why don’t we use it?
In fact, XML naturally has its own strengths. When a complex data structure appears, JSON will be difficult to handle at this time, and XML is the first choice.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn