I have been familiar with JavaScript for a long time, but to be honest, this is the first time I have seen this usage, and I feel ashamed. So, I searched for writing information on the Internet and wrote some examples, hoping to give some help to gardeners.
Function: The function of this function is mainly for serializing objects.
Some people may be allergic to the word serialization, my understanding is very simple. That is to say, convert the original object type into a string type (or to be more precise, json type). It's that simple. For example, if you have a class, you can convert it into the corresponding json type through this method. It's very simple.
Read on.
Syntax:
JSON.stringify(value [, replacer] [, space])
value: is a required field. It is the object you input, such as array, class, etc.
replacer: This is optional. It is divided into 2 ways, one is method, and the second is array.
Situation 1: Let’s talk about the data first. Through our subsequent experiments, we can know that it is related to the first one. Generally speaking, our serialized results are represented by key-value pairs.
For example:
name:"lan",age:25
This form.
So, in this form, if the second value exists in the first one, then the second value will be used as the key, and the first value will be represented as the value. If it does not exist, sorry, neglect. [Isn’t it a bit abstract? I think so too, but you can wait and see the experiment. . Huh. 】
Case 2: If it is a method, it is very simple, that is, passing each serialized object (remember it is each one) into the method for processing.
space: It’s easy to understand what is used as a separator.
1. If omitted, the displayed value will have no separator. Output directly
2. If it is a number, then it defines how many characters to indent. Of course, if it is greater than 10, the maximum value is 10.
3. If it is some escape characters, such as " " , means carriage return, then it has one carriage return per line.
4. If it is just a string, OK, just append these strings to each line when outputting the value. Of course, the maximum length is also 10 characters.
Start with examples;
1. When there is only one parameter:
var student = new Object();
student.name = "Lanny";
student.age = "25";
student.location = "China";
var json = JSON.stringify(student);
alert(student);
The results are as follows:
Some people may I will doubt the role of JSON.stringify, OK. What if, we don't want this function. The code looks like below:
var student = new Object() ;
student.name = "Lanny";
student.age = "25";
student.location = "China";
// var json = JSON.stringify(student );
alert(student);
Congratulations on the result:
I didn’t lie to you, keep going.
2. When the second parameter exists and is still a function
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 one. Both are the same, but the writing method is slightly different. That’s all.
The result is as follows:
3. When 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 also an array, only the value of the first parameter will be displayed.
For example:
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);
sorry The result is:
The second one is ignored, but the first one is serialized.
3.2 If the first one is an object (the object mentioned here is like the new one in C#), the second one is an array.
Then if the second value exists in the first one, then use the second value as the key and the first value as the value to represent
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 obtained is as follows:
Because stu[2] = "Hi"; This Hi cannot be found in the first one, so it will not be displayed.
4. The third parameter
4.1. If omitted, the displayed value will have no separator. Directly output to
For example:
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, then it defines the indentation Several characters, of course if it is greater than 10, the maximum value is 10.
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 here
alert(json);
Then what you get is:
empty Opened 10 characters.
4.3. If it is some escape character, such as " ", indicating a carriage return, then it will have one carriage return per line.
Same thing.
4.4. If it is just a string, OK, just append these strings to each line when outputting the value. Of course, the maximum length is also 10 characters.
If it is var json = JSON.stringify(student,stu,“HaiKou”);//
That’s it. good night.
Article source http://www.cnblogs.com/damonlan/