JSON.parse()方法

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

如下爲json2js中的原文介紹 html

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. java

參數 json

text 數組

必需。 一個有效的 JSON 字符串。 函數


reviver
spa

可選。 一個轉換結果的函數。 將爲對象的每一個成員調用此函數。 若是成員包含嵌套對象,則先於父對象轉換嵌套對象。 對於每一個成員,會發生如下狀況: .net

若是 reviver 返回一個有效值,則成員值將替換爲轉換後的值。
若是 reviver 返回它接收的相同值,則不修改爲員值。
若是 reviver 返回 null 或 undefined,則刪除成員。
orm


返回值 xml

一個對象或數組。


[html]  view plain copy
  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修改返回結果的功能。

相關文章
相關標籤/搜索