search
HomeWeb Front-endJS TutorialJSON 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 (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
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use