摘自: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

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.parse的更多相关文章

  1. JSON.parse()和JSON.stringify()

    1.parse 用于从一个字符串中解析出json 对象.例如 var str='{"name":"cpf","age":"23&q ...

  2. JSON.stringify()与JSON.parse()

    JSON.stringify()用于把一个对象解析成字符串,如 var student = { age: 23, name: 'wang' } JSON.stringify(student); 结果: ...

  3. JSON.parse 与 eval() 对于解析json的问题

    1.eval()与JSOn.parse的不同 eval() var c = 1; //全局变量 var jsonstr1 = '{"name":"a",&quo ...

  4. JSON.parse与eval的区别

    JSON.parse与eval和能将一个字符串解析成一个JSON对象,但还是有挺大区别. 测试代码 var A = "{ a: 1 , b : 'hello' }"; var B ...

  5. ajex请求的数据 什么时候需用Json.parse()

    ajex请求的数据 什么时候需用 Json.parse()

  6. JSON.parse和eval的区别

    JSON.parse和eval的区别 JSON(JavaScript Object Notation)是一种轻量级的数据格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是Jav ...

  7. JSON.stringify()和JSON.parse()

    parse用于从一个字符串中解析出json对象,如 var str = '{"name":"huangxiaojian","age":&qu ...

  8. JSON.parse()和JSON.stringify()区别

    parse用于从一个字符串中解析出json对象,如: var str = '{"name":"huangxiaojian","age":&q ...

  9. JSON.stringify() / JSON.parse()

    JSON.stringify() 这个方法可以把javascript对象转换成json字符串. JSON.parse() 这个方法可以把 json 字符串转换成 javascript对象. [下面来看 ...

  10. js中解析json对象:JSON.parse()用于从一个字符串中解析出json对象, JSON.stringify()用于从一个对象解析出字符串。

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

随机推荐

  1. Hibernate4.1之后关于占位符的问题

    hibernate 4.1之后对于HQL中查询参数的占位符做了改进,如果仍然用老式的占位符会有类似如下的告警信息 [main] WARN [org.hibernate.hql.internal.ast ...

  2. 关系与导航属性(摘自微软MSDN)

    关系与导航属性 本主题概述实体框架如何管理实体间的关系.还对如何映射和操作关系提供了一些指南. 关系.导航属性和外键 在关系数据库中,表之间的关系(也称为关联)是通过外键定义的.外键 (FK) 是用于 ...

  3. 玩玩redis

    一: 介绍 Redis 是一个高性能的key-value数据库. redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部分场合可以对关系数据库起到很好的补充作用.它提 ...

  4. go语言的命令行库

    命令行应用通常很小,程序猿们也不喜欢为它编写注释.所以一些额外的工作,如解析参数有个合理的库来帮忙做就好了.https://github.com/urfave/cli 这个项目因此而生.安装:go g ...

  5. windows7 阻止copyfile到windows目录的解决办法

    一. windows7 x64,uac会阻止copyfile到c:/windows.提示拒绝访问. [会引起uac提示的3种情况: Administrator access token checks. ...

  6. 崽崽帮www.zaizaibang.com精选2

    崔其亮-儿科专科 智慧熊北京丰台幼儿园——秋游活动 周边景点——哈尔滨旅游攻略 月是故乡明丨那些买给自己吃的常德手工月饼老店 [周末乐游]南京最美10所大学,这个周末一起逛一逛吧 彭韶-儿科二门诊 [ ...

  7. javascript介绍

    1.javascript的简介 1.1javascript的特点 1.安全性(不允许方问本地硬盘),它可以做的是信息的动态交互. 2.跨平台.(只要是可以解释js的浏览器都可以执行,与平台无关) 1. ...

  8. javascript冒泡排序一些新实现

    一.冒泡排序 说起冒泡排序,可能每个人都不会陌生,实现思路相当简单明了,就是不停的对数组进行两两比较,将较大(较小)的一项放在前面: 如 var arr = [7, 3, 10, 1, 8, 4, 2 ...

  9. 数据库整合数据报表SQL实战

    协助同事整理sql统计报表. ---建立由avalue的视图,要过滤重复数据 CREATE VIEW vLectAnswerRecord as SELECT t2.OpenID,t2.Qguid,t1 ...

  10. Ubuntu开启22端口

    [http://blog.csdn.net/baple/article/details/39288817] 安装OpenSSH Ubuntu缺省没有安装SSH Server,使用以下命令安装: sud ...