Generate JSON object ----> JS

Source: Internet
Author: User

JSON. stringify:

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 (JSON );

The result is as follows:

Some may suspectJSON. stringifyOK. 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.

For var JSON =JSON. stringify(Student, Stu, "Haikou ");//

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.