JavaScript Advanced Programming: 20th Chapter

Source: Internet
Author: User
Tags tojson

20th Chapter

First, the grammar

The syntax of JSON can represent the following three types of values:

(1) Simple value

(2) Object

There are some differences between JSON objects and JavaScript literals. For example, in JavaScript, the preceding object literal can be written as follows:

var object = {

"Name": "Nicholas",

"Age": 29

} ;

JSON represents the above objects in the following way:

{

"Name": "Nicholas",

"Age": 29

}

The difference: First, there is no declaration of the variable, and secondly, there is no semicolon at the end. Finally, the properties of the object must be enclosed in double quotation marks.

(3) array

Here is the array literal in javascript:

var values = [+, "HI", true];

In JSON, you can use the same syntax to represent the same array:

[]25, "HI", true]

Similarly, there are no variables and semicolons in the JSON array.

Second, parsing and serialization

1.JSON objects

The early JSON parser was basically the eval () function using JavaScript. Because JSON is a subset of JavaScript syntax, the eval () function can parse, interpret, and return JavaScript objects and arrays.

There are two methods of JSON objects: Stringgy () and parse (). In the simplest case, these two methods are used to serialize JavaScript objects into JSON strings and to parse JSON strings into native JavaScript values.

2. Serialization options

Json.stringify () In addition to the JavaScript object to serialize, you can also receive two additional parameters, which are used to specify a different way to serialize the JavaScript object. The first parameter is a filter, which can be an array or a function, and the second argument is an option that indicates whether indentation is preserved in the JSON string.

(1) Filter results

If the filter parameter is an array, then the result of Json.stringify () will contain only the attributes listed in the array. Take a look at the following example:

var book = {

"title": "Professional JavaScript",

"Authors": [

"Nicholas C.zakas"],

Edition:3, year:2011

} ;

var jsontext = json.stringify (book, ["title", "edition"]);

The second argument to Json.stringify () is an array that contains two strings: "title" and "Editon". These two properties correspond to the attributes in the object that will be serialized, so only the two properties are included in the returned result string:

{"title": "Professional JavaScript", "edition": 3}

(2) String indentation

The third parameter of the Json.stringify () method is used to control the indentation and whitespace characters in the result. If this parameter is a numeric value, it represents the number of spaces indented at each level. For example, to indent 4 spaces at each level, you can write code like this:

var book = {

"title": "Professional JavaScript";

"Authors": [

"Nicholas C. Zakas "

] ,

Edition:3,

year:2011

} ;

var jsontext = json.stringify (Nook, NULL, 4);

(3) ToJSON () method

Sometimes, json.stringify () does not satisfy the need for custom serialization of certain objects. In these cases, you can return its own JSON data format by calling the Tojson () method on the object.

ToJSON () can be supplemented as a function filter, so it is important to understand the internal order of serialization. Suppose you pass an object into Json.stringify () and serialize the object in the following order:

The method is called if the Tojson () method is present and can be used to obtain a valid value. Otherwise, the serialization is performed in the default order.

If the second argument is provided, the function filter should be used. The value passed into the function filter is the value returned by step (1).

Each value returned by step (2) is serialized accordingly.

If a third argument is provided, the corresponding formatting is performed.

3. Resolution Options

The Json.parse () method can also receive another parameter, which is a function that will be called on each key-value pair. In order to distinguish the substitution function received by json.stringify (), this function is called a restore function.

JavaScript Advanced Programming: 20th Chapter

Related Article

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.