Introduction to Json.stringify syntax in JavaScript

Source: Internet
Author: User
Tags json serialization
Knowledge of JavaScript is not a short time, but this usage is really, I have seen the first time, ashamed ah ashamed ah. So, I found some information on the internet and wrote some examples to help the garden.
Role: This function is mainly for serialization of objects.
Maybe some people are allergic to serialization, and my understanding is very simple. That is, converting a type that was originally an object to a string type (or, more specifically, a JSON type). It's as simple as that. For example, you have a class, so you can convert it to the corresponding JSON type. It's very simple.
Then look.
Grammar:
Json.stringify (value [, Replacer] [, space])
The code is as follows Copy Code
Value: Is the field that must be required. Are the objects you enter, such as arrays, classes, and so on.
Replacer: This is optional. It is divided into 2 ways, one is the method, the second is the array.
Case one: Let's start with the data, and we can see from the experiments that we have later that it is related to the first one. In general, our serialized results are represented by key-value pairs.
For example:
Name: "LAN", age:25
This form.
So, if this form is the case, if the second value is present in the first, then the second value is the key, the first value is represented by value, and if not, sorry, ignored. "is not a little abstract, I think so, but you wait and see the experiment OK." Whirring 】
Situation Two: If it is a method, it is very simple, that is, the serialization of each object (remember each) into the method of processing.
The code is as follows Copy Code
Space: Well understood, with what to do the separator.
1. If omitted, the displayed value does not have a separator character. Direct output to
2. If it is a number, then it is defined to indent a few characters, of course, if greater than 10, the maximum value is 10.
3. If there are some escape characters, such as "T", which indicates carriage return, then it is a carriage return per line.
4. If it's just a string, OK, just append the string to each line when you output the value. Of course, the maximum length is 10 characters.
Start with an example to illustrate;
1. In the case of only one parameter:
  
  
The code is as follows Copy Code
var student = new Object ();
The code is as follows Copy Code
Student.name = "Lanny";
The code is as follows Copy Code
Student.age = "25";
The code is as follows Copy Code
Student.location = "the";
The code is as follows Copy Code
var json = json.stringify (student);
The code is as follows Copy Code
alert (student);
The results are as follows:
Some people may doubt the role of Json.stringify, OK. Well, if we don't want this function. Following the code:
  
   
   
The code is as follows Copy Code
var student = new Object ();
            Student.name = "Lanny";
            Student.age = "a";
            Student.location = "the";

           var json = json.stringify (student);
            alert (student);
Congratulations on your results:
Don't lie to you, go on.
2. The second parameter exists and the second parameter is a function
  
  
  code is as follows copy code
 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 replaced by the following, 2 are the same, but the wording is a little bit different.
The results were as follows:
3. The second parameter exists, and the second argument is not a function, but an array.
3.1 "Misunderstanding" if the first argument is an array, and the second argument is an array, only the value of the first argument is displayed.
Like what:
  
   
   
The code is as follows Copy Code
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 one was ignored, but the first one was serialized.
3.2 If the first is an object (the object here is like a new one in C #), the second is an array.
So if the value of the second one exists in the first, then the second value is the key, and the first value represents
  
   
   
The code is as follows Copy Code
var student = new Object ();
            STUDENT.QQ = "5485891512";
            Student.name = "Lanny";
            Student.age =;

            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 results obtained are as follows:
Because stu[2] = "HI"; this hi is not found in the first one, so it is not displayed.
4. Third parameter
4.1. If omitted, the displayed value does not have a separator character. Direct output to
Like what:
  
   
   
The code is as follows Copy Code
var student = new Object ();
            STUDENT.QQ = "5485891512";
            Student.name = "Lanny";
            Student.age =;

            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, then it is defined to indent a few characters, of course, if greater than 10, the maximum value is 10.
  
   
   
The code is as follows Copy Code
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 here alert (JSON);
So what you get is:
10 characters were empty.
4.3. If there are some escape characters, such as "T", which indicates carriage return, then it is a carriage return per line.
is the same.
4.4. If it's just a string, OK, just append the string to each line when you output the value. Of course, the maximum length is 10 characters.
If it is var json = json.stringify (Student,stu, "Haikou");
That's it. Good night.
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.