JSON.parse()方法 【轉載】

來源:互聯網
上載者:User

標籤: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修改返回結果的功能。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.