Example of JSON. stringify syntax

Source: Internet
Author: User

It's not a short time to know javascript, But I have met javascript for the first time. As a result, I found some materials on the Internet and wrote some examples to help the garden.
Purpose: this function is mainly used to serialize objects.
Some people may be allergic to serialization. My understanding is simple. That is to say, convert the original object type to the string type (or, more specifically, the json type ). That's simple. For example, if you have a class, you can use this method to convert it to the corresponding json type. It's easy.
Next, let's take a look.
Syntax:
JSON. stringify (value [, replacer] [, space])
Value: A required field. It is the object you enter, such as arrays and classes.
Replacer: This is optional. It can be divided into two methods: method and array.
Scenario 1: Let's talk about data first. We can see from the experiment below that it is related to the first one. Generally, the serialized results are represented by key-value pairs.
For example:
Name: "lan", age: 25
This form.
Therefore, if this form is used, if the second value exists in the first one, then the second value is used as the key, and the first value is the value. If it does not exist, ignore. [Is it a little abstract? I think so too, but it will be okay if you look at the experiment later .. Call .]
Case 2: If it is a method, it is very simple. That is to say, each object after serialization (remember every one) is passed into the method for processing.
Space: well understood, what is used as the separator.
1. If omitted, the displayed value does not have a separator. Direct output
2. If it is a number, it defines the indentation of several characters. Of course, if it is greater than 10, the maximum value is 10.
3. If it is an escape character, for example, "\ t", which indicates a carriage return, one carriage return is returned per line.
4. if it is only a string, OK is used to append these strings to the output values of each row. Of course, the maximum length is also 10 characters.
Start to use instance description;
1. When there is only one parameter:
Copy codeThe Code is as follows:
Var student = new Object ();
Student. name = "Lanny ";
Student. age = "25 ";
Student. location = "China ";
Var json = JSON. stringify (student );
Alert (student );

The result is as follows:

Some may doubt the function of JSON. stringify, OK. Let's say we don't need this function. The Code is as follows:
Copy codeThe Code is as follows:
Var student = new Object ();
Student. name = "Lanny ";
Student. age = "25 ";
Student. location = "China ";

// Var json = JSON. stringify (student );
Alert (student );

Congratulations! The result is:

Don't lie to you. Continue.
2. The second parameter exists, and when the second parameter is still a function
Copy codeThe Code is as follows:
Var students = new Array ();
Students [0] = "Lanny ";
Students [1] = "dong ";
Students [2] = "I love you ";
Var json = JSON. stringify (students, switchUpper );
Function switchUpper (key, value ){
Return value. toString (). toUpperCase ();
}
Alert (json );

// Var json = JSON. stringify (students, function (key, value ){

// Return value. toString (). toUpperCase ();

//});

The above method can also be changed to the following one. The two methods are the same, but the writing method is a little different.
The result is as follows:

3. The second parameter exists, and the second parameter is not a function, but an array.
3.1 [misunderstanding] if the first parameter is an array and the second parameter is an array, only the value of the first parameter is displayed.
For example:
Copy codeThe Code is as follows:
Var students = new Array ();
Students [0] = "Lanny ";
Students [1] = "dong ";
Students [2] = "I love you ";
Var stu = new Array ();
Stu [0] = "1 ";
Stu [1] = "2 ";
Var json = JSON. stringify (students, stu );
Alert (json );

The result of sorry is:

The second is ignored, but the first is serialized.

3.2 if the first one is an object (the object mentioned here is like new in C #), the second is an array.

If the second value exists in the first value, the second value is used as the key, and the first value is the value.
Copy codeThe Code is as follows:
Var student = new Object ();
Student. qq= "5485891512 ";
Student. name = "Lanny ";
Student. age = 25;

Var stu = new Array ();
Stu [0] = "qq ";
Stu [1] = "age ";
Stu [2] = "Hi"; // This student object does not exist.

Var json = JSON. stringify (student, stu );
Alert (json );

The result is as follows:

Because stu [2] = "Hi"; this Hi cannot be found in the first one, so it is not displayed.

4. The third parameter

. If omitted, the displayed value does not have a separator. Direct output

For example:
Copy codeThe Code is as follows:
Var student = new Object ();
Student. qq= "5485891512 ";
Student. name = "Lanny ";
Student. age = 25;

Var stu = new Array ();
Stu [0] = "qq ";
Stu [1] = "age ";
Stu [2] = "Hi ";

Var json = JSON. stringify (student, stu );
Alert (json );

The output is:

4. 2. If it is a number, it defines the indentation of several characters. Of course, if it is greater than 10, the maximum value is 10.
Copy codeThe Code is as follows:
Var student = new Object ();
Student. qq= "5485891512 ";
Student. name = "Lanny ";
Student. age = 25;

Var stu = new Array ();
Stu [0] = "qq ";
Stu [1] = "age ";
Stu [2] = "Hi ";

Var json = JSON. stringify (student, stu, 100); // note the 100
Alert (json );

The result is:

It contains 10 characters.

. If it is an escape character, such as "\ t", it indicates a carriage return, then each line of it will return a carriage return.

The same is true.

4. if it is only a string and OK, it is OK to append these strings to the output values of each row. Of course, the maximum length is also 10 characters.

If var json = JSON. stringify (student, stu, "HaiKou ");//

That's it. Good night.
Article Source http://www.cnblogs.com/damonlan/

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.