JSON.parse

摘自:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

Syntax

JSON.parse(text[, reviver])

Parameters

text
The string to parse as JSON. See the JSON object for a description of JSON syntax.
reviver Optional
If a function, prescribes how the value originally produced by parsing is transformed, before being returned.

Return value

The Object corresponding to the given JSON text.

Exceptions

Throws a SyntaxError exception if the string to parse is not valid JSON.

Examples

Using JSON.parse()

JSON.parse('{}'); // {} JSON.parse('true'); // true JSON.parse('"foo"'); // "foo" JSON.parse('[1, 5, "false"]'); // [1, 5, "false"] JSON.parse('null'); // null

Using the reviver parameter

If a reviver is specified, the value computed by parsing is transformed before being returned. Specifically, the computed value and all its properties (beginning with the most nested properties and proceeding to the original value itself) are individually run through the reviver, which is called with the object containing the property being processed as this and with the property name as a string and the property value as arguments. If the reviver function returnsundefined (or returns no value, e.g. if execution falls off the end of the function), the property is deleted from the object. Otherwise, the property is redefined to be the return value.

If the reviver only transforms some values and no others, be certain to return all untransformed values as-is, otherwise they will be deleted from the resulting object.

JSON.parse('{"p": 5}', (key, value) => typeof value === 'number' ? value * 2 // return value * 2 for numbers : value // return everything else unchanged ); // { p: 10 } JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', (key, value) => { console.log(key); // log the current property name, the last is "". return value; // return the unchanged property value. }); // 1 // 2 // 4 // 6 // 5 // 3 // ""

JSON.parse() does not allow trailing commas

// both will throw a SyntaxError
JSON.parse('[1, 2, 3, 4, ]'); JSON.parse('{"foo" : 1, }');

Specifications

Specification Status Comment
ECMAScript 5.1 (ECMA-262)
The definition of 'JSON.parse' in that specification.
Standard Initial definition. Implemented in JavaScript 1.7.
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'JSON.parse' in that specification.
Standard  
ECMAScript 2017 Draft (ECMA-262)
The definition of 'JSON.parse' in that specification.
Draft  

Browser compatibility

  • Desktop
  • Mobile
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 3.5 (1.9.1) 8.0 10.5 4.0

Gecko-specific notes

Starting Gecko 29 (Firefox 29 / Thunderbird 29 / SeaMonkey 2.26), a malformed JSON string yields a more detailed error message containing the line and column number that caused the parsing error. This is useful when debugging large JSON data.

JSON.parse('[1, 2, 3, 4,]'); // SyntaxError: JSON.parse: unexpected character at // line 1 column 13 of the JSON data

See also

  • JSON.stringify()
posted on 2016-11-30 09:26 布衣天使 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/Roxlin/p/6116328.html

JSON.parse相关推荐

  1. JSON.parse解析特殊字符报错解决方案

    2019独角兽企业重金招聘Python工程师标准>>> 具体案例: 页面点击"下一任务" 会去请求后台,这里出现的问题是有虚拟任务的时候.然后会返回一个map,也 ...

  2. JSON.parse 函数应用 (复制备忘)

    JSON.parse 函数 JSON.parse 函数 (JavaScript) 将 JavaScript 对象表示法 (JSON) 字符串转换为对象. 语法 JSON.parse(text [, r ...

  3. php json.parse,PHP JSON头导致JSON.parse出错(使用jQuery)

    我正在从PHP文件中获取一些JavaScript格式的JSON数据. 要使用这些数据,我使用JSON.parse(json_response),除了在PHP中使用JSON头之外,它都可以工作:head ...

  4. json.parse()和json.stringify()

    json.parse() 用于从一个字符串解析出json对象 var str = '{"name":"huangzhong","age":& ...

  5. JSONObject JSONArray各种用法以及js eval()函数与JSON.parse的区

    2019独角兽企业重金招聘Python工程师标准>>> 一.在后台使用JSONObject对象,并将从数据库中取出来的数据直接使用 JSONObject的put方法放进去,再将这个J ...

  6. JSON.parse()出错解决

    在做微信小程序时遇到的问题,记录一下. 需求: 将购物车页面的商品信息通过url传到确认订单页 复制代码 购物车页面处理过程: 在购物车页面,处理商品信息组装成如下形式: goodsInfoArr = ...

  7. 【前端】JSON.stringfy 和 JSON.parse(待续)

    JSON.stringfy 和 JSON.parse(待续) 支持全局对象JSON的浏览器有:IE8+, FireFox3.5+, Safari4+, Chrome, Opera10.5+ JSON. ...

  8. 理解JSON对象:JSON.parse、 JSON.stringify

    何时是JSON,何时不是JSON? JSON就是一个有特殊规则的字符串,按照这个规则我们就可以把这个字符串解析成JS对象. JSON是设计成描述数据交换格式的,他也有自己的语法,这个语法是JavaSc ...

  9. JSON.parse()和eval()的区别

    json格式非常受欢迎,而解析json的方式通常用JSON.parse()但是eval()方法也可以解析,这两者之间有什么区别呢? JSON.parse()之可以解析json格式的数据,并且会对要解析 ...

  10. JSON.stringify()和JSON.parse()分别是什么

    JSON.stringify() 从一个对象中解析出字符串 JSON.stringify({"a":"1","b":"2" ...

最新文章

  1. MySQL8.0 - 新特性 - 临时表改进
  2. exchange 2013 升级CU15,提示“上次安装完成后没有重启”的提示
  3. java 图片传输方式_Java图像传输方法
  4. 使用Capture画原理图
  5. 五个温度带的分界线_女神建筑师在拿破仑故乡打造的海景别墅,超美!超有温度!【环球设计2225期】...
  6. pandas绘图_pandas与seaborn可视化对比小案例
  7. elementui树状菜单tree_Vue+Element UI 树形控件整合下拉功能菜单(tree + dropdown +input)...
  8. 【云计算】Kubernetes、Marathon等框架需要解决什么样的问题?
  9. 泛型与容器连载(一)泛型的基本概念和原理
  10. python定位相邻节点_Python selenium 父子、兄弟、相邻节点定位方式详解
  11. lstrip在python中是什么意思_为什么氦气吸入后会变声?
  12. 基于Matlab的标记分水岭分割算法
  13. eclipse配置java开发环境_Java中Eclipse开发环境配置
  14. 华为hwics格式产品文档打开方式
  15. Java进阶之路——从初级程序员到架构师,从小工到专家
  16. 2016集训队论文吉如一
  17. 计算机睡眠状态网络是断开的吗,电脑进入睡眠模式会自动断网如何解决
  18. c语言输入奇数n输出一半棱形,c语言输出菱形
  19. android 遥控器 地址码,RK3128平台android系统修改添加遥控器键值码值
  20. python打开读取文件内容

热门文章

  1. canvas 圆角矩形,背景圆角矩形
  2. 23.Vue绑定style样式
  3. Shell中的exit 0 和 exit 1是做什么的 ?
  4. 【define】你知道C语言中define的作用吗?
  5. 转:分心时代,有这三种专注力的人就是最稀缺的资源
  6. Linux下的按键输入驱动开发
  7. 95号段短号码和短消息接入代码的禁止要求是什么?
  8. 耶鲁大学计算机科学录取,重磅!!!耶鲁大学计算机科学录取+1!
  9. VMware虚拟机安装 Linux(Ubuntu)教程
  10. COM病毒概述以及实验操作