Original address: http://blog.csdn.net/lowkeysk/article/details/8175195 thank the author
This article describes the parse () method in Json2.js of JavaScript in JSON.
The following is an introduction to the original text in Json2js
Json.parse (text, reviver)
This method parses a JSON text to produce an object or array.
It can throw a SyntaxError exception.
The optional reviver parameter is a function that can filter and
Transform the results. It receives each of the keys and values,
And its return value is used instead of the original value.
If it returns what it received and then the structure was not modified.
If it returns undefined then the member is deleted.
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.
This article describes the parse () method in Json2.js of JavaScript in JSON.
The following is an introduction to the original text in Json2js
Json.parse (text, reviver)
This method parses a JSON text to produce an object or array.
It can throw a SyntaxError exception.
The optional reviver parameter is a function that can filter and
Transform the results. It receives each of the keys and values,
And its return value is used instead of the original value.
If it returns what it received and then the structure was not modified.
If it returns undefined then the member is deleted.
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.
[HTML]View Plaincopy
- <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
- <HTML xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>json.parse ()</title>
- <script type="Text/javascript" src="Json2.js"></script>
- <script type="Text/javascript">
- var data=' {'
- + ' root ': '
- + ' ['
- + ' {' name ': ' 1 ', ' value ': ' 0 '}, '
- + ' {' name ': ' 6101 ', ' value ': ' Xian City '}, '
- + ' {' name ': ' 6102 ', ' value ': ' Tongchuan '}, '
- + ' {' name ': ' 6103 ', ' value ': ' Baoji '}, '
- + ' {' name ': ' 6104 ', ' value ': ' Xianyang '}, '
- + ' {' name ': ' 6105 ', ' value ': ' Weinan '}, '
- + ' {' name ': ' 6106 ', ' value ': ' Yanan '}, '
- + ' {' name ': ' 6107 ', ' value ': ' Hanzhong '}, '
- + ' {' name ': ' 6108 ', ' value ': ' Yulin '}, '
- + ' {' name ': ' 6109 ', ' value ': ' Ankang '}, '
- + ' {' name ': ' 6110 ', ' value ': ' Shangluo '} '
- + '] '
- + '} ';
- Example 1: This example uses Json.parse to convert a JSON string to an object
- var jsontext = ' {' FirstName ': "Jesper", "surname": "Aaberg", "Phone": ["555-0100", "555-0120"]} ';
- var contact = Json.parse (jsontext);
- document.write (Contact.surname + "," + Contact.firstname + "," + Contact.phone);
- Datereviver
- var dateobj = new Date (' DATE.UTC ', + ' 1, + ' + ', + ' + ', + ' xx ', + ' xx ')
- Alert (dateobj.toutcstring ())
- Example 2: This example uses the Json.parse to deserialize a date string in ISO format and returns a date format object.
- var jsontext2 = ' {' hiredate ': ' 2008-01-01t12:00:00z ', ' birthdate ': ' 2008-12-25t12:00:00z '} ';
- var dates = json.parse (jsontext2, datereviver);
- document.write ("<br /><br />" +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;
- };
- </Script>
- </head>
- <body>
- </body>
- </html>
There are two examples in the code above:
Example 1 provides the ability to convert a JSON string into a JSON object. Note The format of the JSON string must be standard, key and value must be enclosed in double quotation marks, otherwise it will parse the exception.
The example 2 feature describes reviver modifying the ability to return results.