Original address http://www.jb51.net/article/29893.htm Thank you
It is not a short time to know JavaScript, but this usage is true, I have seen it for the first time, ashamed ah ashamed ah. Thus, on the Internet to write information, wrote some examples hope to give some help to the garden.
role: This function is mainly for the purpose of serialization objects.
Maybe some people are allergic to the word serialization, my understanding is very simple. This means converting the type of the object to a string type (or, more specifically, the JSON type). It's so simple. For example, you have a class, so you can convert it to the corresponding JSON type by this method. It's simple.
Then look.
Syntax:
Json.stringify (value [, Replacer] [, space])
Value: is the required field. Is the object you have entered, such as array Ah, class AH and so on.
Replacer: This is optional. It is divided into 2 ways, one is method, the second is an array.
Situation one: We first say the data, through the experiment behind us, we can know that it is related to the first one. In general, our serialization results are represented by key-value pairs.
For example:
Name: "LAN", age:25
This form.
So, if this is the case, if the value of the second exists in the first, then the value of the second is the key, the first value for value is represented, if it does not exist, sorry, ignore. "It's not a bit abstract, I think so, but you just wait and see the experiment is OK." Whirring 】
Situation Two: If it is a method, it is very simple, that is, the serialization of each object (remember that every one) into the method of processing.
Space: Well understood, with what to do the delimiter.
1. If omitted, then 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 it is greater than 10, the maximum value is 10.
3. If there are some escape characters, such as "\ T", which means carriage return, then it is a carriage return per line.
4. If it is just a string, OK, append the strings to the output value in each row. Of course, the maximum length is also 10 characters.
begin to illustrate with examples;
1. In the case of only one parameter:
?
1234567 |
var obj={ webName: "脚本之家" , url: "jb51.net" , age: "2" } var str=JSON.stringify(obj) console.log(str); |
The above code implements the conversion function, which is json.stringify (), which is the new method of ECMAScript5.
There are certain browser compatibility issues, as follows:
(1). This method is supported by browsers IE8 and IE8.
(2). Google Chrome supports this method.
(3). Firefox supports this method.
(4). Opera browser supports this method.
(5). This method is supported by the Safria browser.
Or
?
123456 |
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 doubt the role of Json.stringify, OK. Well, if we don't want this function. The code looks like this:
?
1234567 |
var student = new Object(); student.name = "Lanny" ; student.age = "25" ; student.location = "China" ; // var json = JSON.stringify(student); alert(student); |
Congratulations on the results you've got:
Don't lie to you, go on.
2. The second parameter is present, and the second argument is a function.
?
123456789101112131415 |
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 of a little bit different.
The results were 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, the second argument is an array, only the value of the first parameter is displayed.
Like what:
?
123456789 |
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 results are as follows:
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 in C #), the second one is an array.
Then if the value of the second exists in the first, then the second value is the key, and the first value is represented as value.
?
123456789101112 |
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"
;
//这个student对象里不存在。 var json = JSON.stringify(student,stu);
alert(json);
|
The results are as follows:
Because stu[2] = "HI", this hi is not found in the first, so it is not displayed.
4. A third parameter
4.1. If omitted, then the displayed value does not have a separator character. Direct output to
Like what:
?
123456789101112 |
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 is defined to indent a few characters, of course, if it is greater than 10, the maximum value is 10.
?
123456789101112 |
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);
//注意这里的100
alert(json);
|
So what you get is:
10 characters are vacated.
4.3. If there are some escape characters, such as "\ T", which means carriage return, then it is a carriage return per line.
is the same.
4.4. If it is just a string, OK, append the strings to the output value in each row. Of course, the maximum length is also 10 characters.
If it is var json = json.stringify (Student,stu, "Haikou");//
Json.stringify Syntax Example explanation (GO)