JSON format detailed __JS

Source: Internet
Author: User

JSON format: http://www.json.org/

Python and JSON relations please refer to: http://docs.python.org/library/json.html

There are two structures of JSON construction:

1. Collection of name/value pairs (A collection of name/value pairs). In different languages, it is understood as objects (object), records (record), structure (struct), dictionaries (dictionary), hash tables (hash table), a list of keys (keyed list), or associative arrays (associative Array).

2. List of values (an ordered list of values). In most languages, it is understood as an array. Basic Example

Simply put, JSON converts a set of data represented in a JavaScript object to a string, which can then be easily passed between functions, or pass a string from a WEB client to a server-side program in an asynchronous application. This string looks a little odd, but JavaScript is easy to explain, and JSON can represent a more complex structure than name/value pairs. For example, you can represent arrays and complex objects, not just simple lists of keys and values. represents a name/value pair

In the simplest form, you can represent a name/value pair using JSON such as the following:

{"FirstName": "Brett"}

This example is very basic and actually takes up more space than the equivalent plain text "name/value pair":

Firstname=brett

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:

{"FirstName": "Brett", "LastName": "McLaughlin", "email": "AAAA"}

In terms of syntax, this is not a great advantage compared to 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. represents an array

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:

{"People": [

{"FirstName": "Brett", "LastName": "McLaughlin", "email": "AAAA"},

{"FirstName": "Jason", "LastName": "Hunter", "email": "BBBB"},

{"FirstName": "Elliotte", "LastName": "Harold", "email": "CCCC"}

]}

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": [

{"FirstName": "Brett", "LastName": "McLaughlin", "email": "AAAA"},

{"FirstName": "Jason", "LastName": "Hunter", "email": "BBBB"},

{"FirstName": "Elliotte", "LastName": "Harold", "email": "CCCC"}

],

"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"}

] }

Most notable here is the ability to 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. Format Application

Once you have the JSON format, it's easy to use it in JavaScript. JSON is the native format of JavaScript, which means that processing JSON data in JavaScript does not require any special APIs or toolkits. assigning JSON data to a variable

For example, you can create a new JavaScript variable and then assign it directly to the JSON-formatted data string:

var people = {"Programmers": [{"FirstName": "Brett", "LastName": "McLaughlin", "email": "AAAA"},

{"FirstName": "Jason", "LastName": "Hunter", "email": "BBBB"},

{"FirstName": "Elliotte", "LastName": "Harold", "email": "CCCC"}

],

"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"}

] }

This is very simple; now people contains the JSON-formatted data that you saw earlier. But that's not enough, because the way the data is accessed doesn't seem obvious. Accessing Data

Although it doesn't look obvious, the long string above is actually just an array, and after you put the array in a JavaScript variable, you can easily access it. In fact, you just use dot notation to represent an array element. So, to access the last name of the first entry in the programmers list, just use the following code in JavaScript:

People.programmers[0].lastname;

Note that the array index is zero-based. So, this line of code first accesses the data in the people variable, moves to the entry called programmers, moves to the first record ([0]), and finally accesses the value of the LastName key. The result is the string value "McLaughlin".

Here are several examples of using the same variable.

People.authors[1].genre//Value is "fantasy"

People.musicians[3].lastname//Undefined. This is refers to the fourth entry, and there isn ' t one

People.programmers[2].firstname//Value is "Elliotte"

With this syntax, you can work with any JSON-formatted data without using any additional JavaScript toolkits or APIs. Modifying JSON data

Just as you can access data with dots and parentheses, you can easily modify the data in the same way:

People.musicians[1].lastname = "Rachmaninov";

After you convert a string to a JavaScript object, you can modify the data in the variable like this. convert back to String

Of course, if you can't easily convert an object back to the text format mentioned in this article, all data modifications are not of much value. This conversion is also simple in JavaScript:

String Newjsontext = people.tojsonstring ();

That's fine. You now get a text string that you can use anywhere, for example, as a request string in an Ajax application.

More importantly, you can convert any JavaScript object to JSON text. It is not possible to handle only variables that were previously assigned with a JSON string. In order to convert an object named MyObject, you only need to perform the same form of command:

String Myobjectinjson = myobject.tojsonstring ();

This is the biggest difference between JSON and other data formats discussed in this series. If you use JSON, simply call a simple function, you can get the formatted data, you can use it directly. For other data formats, you need to convert between the raw data and the formatted data. Even if you use an API such as Document object Model, which provides a function to convert your data structure to text, you need to learn the API and use the object of the API, rather than using native JavaScript objects and syntax.

The final conclusion is that JSON is almost certainly a good choice if you are dealing with a large number of JavaScript objects, so that you can easily convert the data into a format that can be sent to server-side programs in the request. Concrete Form

1, the object is an unordered "' name/value ' Pair" collection. An object begins with "{" (opening parenthesis), and "}" (closing parenthesis) ends. Each "name" is followed by a ":" (a colon), and the ' name/value ' pair is separated by a ', ' (comma). (as shown in the figure, the way in which the data is represented is similar to the form of non-deterministic automata, and people who have not learned how to compile the principle may be difficult to understand, and in fact the form of regular expressions.) Same

2. An array is an ordered set of values (value). An array begins with "[" (left bracket), and "]" (right bracket) ends. Values are separated by the "," (comma) value.

3. Values (value) can be strings (string), numeric values (number), True, False, NULL, objects (object), or arrays (array) enclosed in double quotes. These structures can be nested.

4. A string is a collection of any number of Unicode characters surrounded by double quotes, which are escaped using backslashes. A character (character) is a separate string (character string). A string is very similar to a C or Java string.

5, the value (number) is also similar to the C or Java values. Remove unused octal and hexadecimal formatting. Remove some coding details.

Live together,or Die alone!

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.