Json.parse () and Json.stringify () methods

Source: Internet
Author: User

parse is used to parse a JSON object from a string , such as var str = ' {' name ': ' Huangxiaojian ', ' age ': ' + '} ' "  "Huangxiaojian"__proto__: Object Note: The single quote is written outside {}, each property name must be in double quotation marks, otherwise an exception will be thrown. stringify () is used to parse out a string from an object , such as   = {a:1,b:2} result: Json.stringify (a)"{" A ": 1," B ": 2 }"

Where Json.stringify's syntax:

Json.stringify (value [, Replacer] [, space])

    • Value: is a required field. Is the object you enter, such as arrays, classes, and so on.
    • Replacer: This is optional. It is divided into 2 different ways, one is an array, the second is a method.

Case one: When the replacer is an array, it is possible to know from the subsequent experiment that it is related to the first parameter, value. In general, serialization results are represented by key-value pairs. Therefore, if the value of the second parameter is present at the first, then the value of the second parameter is the key, the value of the first parameter is the expression of value, and if it does not exist, it is ignored.

Situation two: Replacer as a method, that is very simple, that is, the serialization of each object (remember that every one) into the method of processing.

    • Space: It's what you use to make separators.

1) If omitted, then the displayed value is no delimiter, output directly.
2) If it is a number, then it is defined to indent a few characters, of course, if it is greater than 10, the default is 10, because the maximum value is 10.
3) If there are some escape characters, such as "\ T", which means enter, then it is a carriage return per line.
4) If it is just a string, append these strings to each line of output values. Of course, the maximum length is also 10 characters.

The example illustrates the following:

  1) Only the first parameter of the case

1var student = new Object (); Student.name = "Lanny"; Student.age = "25"; student.location = "China"; var json = json.stringify (student); alert (JSON); alert (student);

Results such as:

Some people may doubt the role of json.stringify. So if we don't want this function, and the direct alert (student), the result is as follows:

This time realize the role of json.stringify.

  2) The second parameter is present, and the second argument is a function

2var students = new Array (); Students[0] = "OnePiece"; STUDENTS[1] = "Naruto"; STUDENTS[2] = "Bleach"; var json = json.stringify (students,switchupper); function Switchupper (key, value) {     return value.tostring (). toUpperCase ();} alert (JSON);/* The following can also be var json = Json.stringify (students, function (Key,value) {return value.tostring (). toUpperCase ()}); alert (JSON); */

The results of the operation are as follows:

 3) The second parameter is present, and the second parameter is not a function, but an array.

The results of the operation are as follows:

The second parameter is ignored, but the first parameter is serialized.

4) If the first argument is an object, the second argument is the case of an array

The results of the operation are as follows:

The third parameter is the output result of the number:

The third parameter is the escape character \ t when the output results:

The third argument is the output of a string:

Reference: http://www.cnblogs.com/ningvsban/p/3660654.html

Json.parse () and Json.stringify () methods

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.