Json.stringfy () and Json.parse ()

Source: Internet
Author: User

Json.stringfy () Converts an object, an array, to Json;json.parse () to convert the JSON into an object.

Json.stringfy ():

Grammar:
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 following is illustrated with examples;

  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:

Resources:

Http://www.jb51.net/article/29893.htm

Json.parse ():

Grammar
JSON.parse(text [, reviver])
Parameters
Text

Necessary. a valid JSON string.

Reviver

Optional. a function that transforms the result. This function is called for each member of the object. if the member contains nested objects, the nested objects are converted before the parent object. for each member, the following conditions occur:

  • If reviver returns a valid value, the member value is replaced with the converted value.

  • If reviver returns the same value it receives, the member value is not modified.

  • If reviver returns null or undefined, the member is deleted.

return value

An object or an array.

Abnormal

The following example uses Json.parse to convert a JSON string into an object.

var jsontext = ' {' FirstName ': "Jesper", "surname": "Aaberg", "Phone": ["555-0100", "555-0120"]} ';
var contact = Json.parse (jsontext);
document.write (Contact.surname + "," + contact.firstname);

Output:aaberg, Jesper

The following example demonstrates how to use json.stringify to convert an array to a JSON string and then use json.parse to convert the string to a group.

var arr = ["A", "B", "C"];
var str = json.stringify (arr);
document.write (str);
document.write ("<br/>");

var newArr = json.parse (str);

while (Newarr.length > 0) {
document.write (Newarr.pop () + "<br/>");
}


Output:
["A", "B", "C"]
C
B
A

The reviver function is typically used to convert the JSON representation of a date string from the International Organization for Standardization (ISO) to a Coordinated Universal Time (UTC) format Date object. This example uses Json.parse to deserialize a date string in ISO format. The Datereviver function returns a Date object for a member formatted as an ISO date string .

var jsontext = ' {' hiredate ': ' 2008-01-01t12:00:00z ', ' birthdate ': ' 2008-12-25t12:00:00z '} ';
var dates = Json.parse (Jsontext, datereviver);
document.write (Dates.birthdate.toUTCString ());

function Datereviver (key, value) {
var A;
if (typeof value = = = ' String ') {
A =/^ (\d{4})-(\d{2})-(\d{2}) T (\d{2}):(\d{2}):(\d{2} (?: \. \d*)?) Z$/.exec (value);
if (a) {
return new Date (DATE.UTC (+a[1], +a[2]-1, +a[3], +a[4],
+A[5], +a[6]);
}
}
return value;
};

Output:
Thu, Dec 12:00:00 UTC

Reference: https://technet.microsoft.com/zh-cn/sysinternals/cc836466 (en-us,vs.85). aspx

Json.stringfy () and Json.parse ()

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.