Json.parse () method "Reprint"

Source: Internet
Author: User

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
  1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
  2. <HTML xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>json.parse ()</title>
  5. <script type="Text/javascript" src="Json2.js"></script>
  6. <script type="Text/javascript">
  7. var data=' {'
  8. + ' root ': '
  9. + ' ['
  10. + ' {' name ': ' 1 ', ' value ': ' 0 '}, '
  11. + ' {' name ': ' 6101 ', ' value ': ' Xian City '}, '
  12. + ' {' name ': ' 6102 ', ' value ': ' Tongchuan '}, '
  13. + ' {' name ': ' 6103 ', ' value ': ' Baoji '}, '
  14. + ' {' name ': ' 6104 ', ' value ': ' Xianyang '}, '
  15. + ' {' name ': ' 6105 ', ' value ': ' Weinan '}, '
  16. + ' {' name ': ' 6106 ', ' value ': ' Yanan '}, '
  17. + ' {' name ': ' 6107 ', ' value ': ' Hanzhong '}, '
  18. + ' {' name ': ' 6108 ', ' value ': ' Yulin '}, '
  19. + ' {' name ': ' 6109 ', ' value ': ' Ankang '}, '
  20. + ' {' name ': ' 6110 ', ' value ': ' Shangluo '} '
  21. + '] '
  22. + '} ';
  23. Example 1: This example uses Json.parse to convert a JSON string to an object
  24. var jsontext = ' {' FirstName ': "Jesper", "surname": "Aaberg", "Phone": ["555-0100", "555-0120"]} ';
  25. var contact = Json.parse (jsontext);
  26. document.write (Contact.surname + "," + Contact.firstname + "," + Contact.phone);
  27. Datereviver
  28. var dateobj = new Date (' DATE.UTC ', + ' 1, + ' + ', + ' + ', + ' xx ', + ' xx ')
  29. Alert (dateobj.toutcstring ())
  30. Example 2: This example uses the Json.parse to deserialize a date string in ISO format and returns a date format object.
  31. var jsontext2 = ' {' hiredate ': ' 2008-01-01t12:00:00z ', ' birthdate ': ' 2008-12-25t12:00:00z '} ';
  32. var dates = json.parse (jsontext2, datereviver);
  33. document.write ("<br /><br />" +dates.birthdate.toutcstring ());
  34. function Datereviver (key, value) {
  35. var A;
  36. if (typeof value = = = ' String ') {
  37. a =/^ (\d{4})-(\d{2})-(\d{2}) T (\d{2}):(\d{2}):(\d{2} (?: \. \d*)?)  Z$/.exec (value);
  38. if (a) {
  39. return new Date (DATE.UTC (+a[1], +a[2]-1, +a[3], +a[4],
  40. +A[5], +a[6]);
  41. }
  42. }
  43. return value;
  44. };
  45. </Script>
  46. </head>
  47. <body>
  48. </body>
  49. </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.

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.