JSON.parse()方法
2016-03-23 14:27
417 查看
本文章介绍一下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功能为将json字符串转化为json对象。(注意!json字符串的格式一定要标准,key和value一定要用双引号包括,否则会出线解析异常)
示例2功能介绍reviver修改返回结果的功能。
以下为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,则删除成员。
返回值
一个对象或数组
<!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修改返回结果的功能。
相关文章推荐
- JS自定义属性兼容
- knockout js动态绑定viewModel的对象属性
- js处理select操作总结
- 理解和使用 JavaScript 中的回调函数
- [转]浅谈 JavaScript的原型对象与原型链
- 学习html,css,js记录(1)-html标签说明
- 事件捕获和事件冒泡
- Web报表工具FineReport的JS API开发(二)
- JavaScript中的setInterval用法
- BZOJ1452 [JSOI2009] Count
- Javascript事件总结
- 关于 JSTL c:out 对 XML html 自动转义 转义的禁用
- Servlet/JSP提供了一个实用的技术
- 调整JSP和servlet的一些非常实用的方法
- [Javascript] 怎么判断变量是否不为空
- Jsp1
- JavaScript数据格式验证探讨
- 两个重叠的div,点击内div而不触发外div的点击事件
- javascript 方法的书写
- js中:Date.utc()方法与getTime()方法返回值不相等的原因