码迷,mamicode.com
首页 > Web开发 > 详细

JSON.parse()方法 【转载】

时间:2014-08-05 11:07:59      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:blog   http   java   使用   io   strong   for   art   

原文地址: http://blog.csdn.net/lowkeysk/article/details/8175195      感谢作者

 

 

本文章介绍一下javascript in json 中 json2.js中的parse()方法。

以下为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, then the structure is not modified.
            If it returns undefined then the member is deleted.

参数

text

必需。 一个有效的 JSON 字符串。


reviver

可选。 一个转换结果的函数。 将为对象的每个成员调用此函数。 如果成员包含嵌套对象,则先于父对象转换嵌套对象。 对于每个成员,会发生以下情况:

如果 reviver 返回一个有效值,则成员值将替换为转换后的值。
如果 reviver 返回它接收的相同值,则不修改成员值。
如果 reviver 返回 null 或 undefined,则删除成员。

 

返回值

一个对象或数组。

 

 

本文章介绍一下javascript in json 中 json2.js中的parse()方法。

以下为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, then the structure is not modified.
            If it returns undefined then the member is deleted.

参数

text

必需。 一个有效的 JSON 字符串。


reviver

可选。 一个转换结果的函数。 将为对象的每个成员调用此函数。 如果成员包含嵌套对象,则先于父对象转换嵌套对象。 对于每个成员,会发生以下情况:

如果 reviver 返回一个有效值,则成员值将替换为转换后的值。
如果 reviver 返回它接收的相同值,则不修改成员值。
如果 reviver 返回 null 或 undefined,则删除成员。

 

返回值

一个对象或数组。

 
 
[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":"西安市"},‘   
  12.     +‘{"name":"6102","value":"铜川市"},‘   
  13.     +‘{"name":"6103","value":"宝鸡市"},‘  
  14.     +‘{"name":"6104","value":"咸阳市"},‘   
  15.     +‘{"name":"6105","value":"渭南市"},‘  
  16.     +‘{"name":"6106","value":"延安市"},‘   
  17.     +‘{"name":"6107","value":"汉中市"},‘   
  18.     +‘{"name":"6108","value":"榆林市"},‘   
  19.     +‘{"name":"6109","value":"安康市"},‘   
  20.     +‘{"name":"6110","value":"商洛市"}‘   
  21.     +‘]‘  
  22.     +‘}‘;   
  23.   
  24.   
  25.       
  26.     //示例1:此示例使用 JSON.parse 将 JSON 字符串转换为对象  
  27.     var jsontext = ‘{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}‘;  
  28.     var contact = JSON.parse(jsontext);  
  29.     document.write(contact.surname + ", " + contact.firstname + ", "+ contact.phone);  
  30.   
  31.       
  32.       
  33.     //dateReviver  
  34.     //var dateObj = new Date(Date.UTC(‘2008‘, +‘01‘ - 1, +‘01‘, +‘12‘, +‘00‘, +‘00‘))  
  35.     //alert(dateObj.toUTCString())  
  36.   
  37.     //示例2:此示例使用 JSON.parse 反序列化 ISO 格式的日期字符串, 将返回Date格式对象。  
  38.     var jsontext2 = ‘{ "hiredate": "2008-01-01T12:00:00Z", "birthdate": "2008-12-25T12:00:00Z" }‘;  
  39.     var dates = JSON.parse(jsontext2, dateReviver);  
  40.     document.write("<br /><br />"+dates.birthdate.toUTCString());  
  41.     function dateReviver(key, value) {  
  42.         var a;  
  43.         if (typeof value === ‘string‘) {  
  44.             a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);  
  45.             if (a) {  
  46.                 return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],  
  47.                                 +a[5], +a[6]));  
  48.             }  
  49.         }  
  50.         return value;  
  51.     };  
  52.   
  53. </script>  
  54. </head>  
  55. <body>  
  56. </body>  
  57. </html>  

 

上面代码中有两个示例:

示例1功能为将json字符串转化为json对象。(注意!json字符串的格式一定要标准,key和value一定要用双引号包括,否则会出线解析异常)

示例2功能介绍reviver修改返回结果的功能。

 

JSON.parse()方法 【转载】,布布扣,bubuko.com

JSON.parse()方法 【转载】

标签:blog   http   java   使用   io   strong   for   art   

原文地址:http://www.cnblogs.com/juhualang/p/3891648.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!