JSON literacy post JSON.as class tutorial_json
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 (
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:
{ "firstName": "Brett" }
Copy Code This example is very basic and actually Takes up more space than the equivalent plain text name/value pair:
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:
{ "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:
{ "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": [
{ "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:
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:
{ "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:
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:
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The Gson@Expose annotation can be used to mark whether a field is exposed (contained or not) for serialization or deserialization. The @Expose annotation can take two parameters, each parameter is a boolean value and can take the value true or false. In order for GSON to react to the @Expose annotation, we have to create a Gson instance using the GsonBuilder class and need to call the excludeFieldsWithoutExposeAnnotation() method, which configures Gson to exclude all fields without Expose annotation from serialization or deserialization. Syntax publicGsonBuilderexclud

The combination of golangWebSocket and JSON: realizing data transmission and parsing In modern Web development, real-time data transmission is becoming more and more important. WebSocket is a protocol used to achieve two-way communication. Unlike the traditional HTTP request-response model, WebSocket allows the server to actively push data to the client. JSON (JavaScriptObjectNotation) is a lightweight format for data exchange that is concise and easy to read.

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

MySQL5.7 and MySQL8.0 are two different MySQL database versions. There are some main differences between them: Performance improvements: MySQL8.0 has some performance improvements compared to MySQL5.7. These include better query optimizers, more efficient query execution plan generation, better indexing algorithms and parallel queries, etc. These improvements can improve query performance and overall system performance. JSON support: MySQL 8.0 introduces native support for JSON data type, including storage, query and indexing of JSON data. This makes processing and manipulating JSON data in MySQL more convenient and efficient. Transaction features: MySQL8.0 introduces some new transaction features, such as atomic

Use the json.MarshalIndent function in golang to convert the structure into a formatted JSON string. When writing programs in Golang, we often need to convert the structure into a JSON string. In this process, the json.MarshalIndent function can help us. Implement formatted output. Below we will explain in detail how to use this function and provide specific code examples. First, let's create a structure containing some data. The following is an indication

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

Using PHP's json_encode() function to convert an array or object into a JSON string and format the output can make it easier to transfer and exchange data between different platforms and languages. This article will introduce the basic usage of the json_encode() function and how to format and output a JSON string. 1. Basic usage of json_encode() function The basic syntax of json_encode() function is as follows: stringjson_encod
