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