加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JSON.parse()方法

发布时间:2020-12-16 19:03:36 所属栏目:百科 来源:网络整理
导读:本文章介绍一下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 param

本文章介绍一下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 plain copy
  1. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <htmlxmlns="http://www.w3.org/1999/xhtml">
  3. headtitle>JSON.parse()</scripttype="text/javascript"src="json2.js">scriptscripttype="text/javascript" vardata='{'
  4. +'"root":'
  5. +'['
  6. +'{"name":"1","value":"0"},'
  7. +'{"name":"6101","value":"西安市"},'
  8. +'{"name":"6102","value":"铜川市"},108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +'{"name":"6103","value":"宝鸡市"},248)"> +'{"name":"6104","value":"咸阳市"},108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +'{"name":"6105","value":"渭南市"},248)"> +'{"name":"6106","value":"延安市"},108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +'{"name":"6107","value":"汉中市"},248)"> +'{"name":"6108","value":"榆林市"},108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +'{"name":"6109","value":"安康市"},248)"> +'{"name":"6110","value":"商洛市"}'
  9. +']'
  10. +'}';
  11. //示例1:此示例使用JSON.parse将JSON字符串转换为对象
  12. varjsontext='{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';
  13. varcontact=JSON.parse(jsontext);
  14. document.write(contact.surname+","+contact.firstname+","+contact.phone);
  15. //dateReviver
  16. //vardateObj=newDate(Date.UTC('2008',+'01'-1,+'01',+'12',+'00',+'00'))
  17. //alert(dateObj.toUTCString())
  18. //示例2:此示例使用JSON.parse反序列化ISO格式的日期字符串,将返回Date格式对象。
  19. varjsontext2='{"hiredate":"2008-01-01T12:00:00Z","birthdate":"2008-12-25T12:00:00Z"}';
  20. vardates=JSON.parse(jsontext2,dateReviver);
  21. document.write("br/>/>"+dates.birthdate.toUTCString());
  22. functiondateReviver(key,value){
  23. vara;
  24. if(typeofvalue==='string'){
  25. a=/^(d{4})-(d{2})-(d{2})T(d{2}):(d{2}):(d{2}(?:.d*)?)Z$/.exec(value);
  26. if(a){
  27. returnnewDate(Date.UTC(+a[1],+a[2]-1,+a[3],+a[4],
  28. +a[5],+a[6]));
  29. }
  30. }
  31. returnvalue;
  32. };
  33. bodyhtml>


上面代码中有两个示例:

示例1功能为将json字符串转化为json对象。(注意!json字符串的格式一定要标准,key和value一定要用双引号包括,否则会出线解析异常)

示例2功能介绍reviver修改返回结果的功能。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读