JSON.parse()方法

本文章介紹一下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,則刪除成員。


返回值

一個對象或數組。


  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>  
<!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":"西安市"},' 
	+'{"name":"6102","value":"銅川市"},' 
	+'{"name":"6103","value":"寶雞市"},'
	+'{"name":"6104","value":"咸陽市"},' 
	+'{"name":"6105","value":"渭南市"},'
	+'{"name":"6106","value":"延安市"},' 
	+'{"name":"6107","value":"漢中市"},' 
	+'{"name":"6108","value":"榆林市"},' 
	+'{"name":"6109","value":"安康市"},' 
	+'{"name":"6110","value":"商洛市"}' 
	+']'
	+'}'; 


	
	//示例1:此示例使用 JSON.parse 將 JSON 字符串轉換爲對象
	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('2008', +'01' - 1, +'01', +'12', +'00', +'00'))
	//alert(dateObj.toUTCString())

	//示例2:此示例使用 JSON.parse 反序列化 ISO 格式的日期字符串, 將返回Date格式對象。
	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>


上面代碼中有兩個示例:

示例1功能爲將json字符串轉化爲json對象。(注意!json字符串的格式一定要標準,key和value一定要用雙引號包括,否則會出線解析異常)

示例2功能介紹reviver修改返回結果的功能。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章