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
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的更多相关文章
- JSON.parse()和JSON.stringify()
1.parse 用于从一个字符串中解析出json 对象.例如 var str='{"name":"cpf","age":"23&q ...
- JSON.stringify()与JSON.parse()
JSON.stringify()用于把一个对象解析成字符串,如 var student = { age: 23, name: 'wang' } JSON.stringify(student); 结果: ...
- JSON.parse 与 eval() 对于解析json的问题
1.eval()与JSOn.parse的不同 eval() var c = 1; //全局变量 var jsonstr1 = '{"name":"a",&quo ...
- JSON.parse与eval的区别
JSON.parse与eval和能将一个字符串解析成一个JSON对象,但还是有挺大区别. 测试代码 var A = "{ a: 1 , b : 'hello' }"; var B ...
- ajex请求的数据 什么时候需用Json.parse()
ajex请求的数据 什么时候需用 Json.parse()
- JSON.parse和eval的区别
JSON.parse和eval的区别 JSON(JavaScript Object Notation)是一种轻量级的数据格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是Jav ...
- JSON.stringify()和JSON.parse()
parse用于从一个字符串中解析出json对象,如 var str = '{"name":"huangxiaojian","age":&qu ...
- JSON.parse()和JSON.stringify()区别
parse用于从一个字符串中解析出json对象,如: var str = '{"name":"huangxiaojian","age":&q ...
- JSON.stringify() / JSON.parse()
JSON.stringify() 这个方法可以把javascript对象转换成json字符串. JSON.parse() 这个方法可以把 json 字符串转换成 javascript对象. [下面来看 ...
- js中解析json对象:JSON.parse()用于从一个字符串中解析出json对象, JSON.stringify()用于从一个对象解析出字符串。
JSON.parse()用于从一个字符串中解析出json对象. var str = '{"name":"huangxiaojian","age&quo ...
随机推荐
- PHP中的运算符---算术运算符、逻辑运算符、赋值运算符、比较运算符
1.算术运算符 常见的算术运算符 运算类型 运算符 举例 结果 取反运算 - -$a 返回$a的负值 加法运算 + $a + $b 返回$a与$b的和 减法运算 - $a - $b 返回$a与$b的差 ...
- Quartz2D之绘制一个简单的机器猫
学习iOS有一段时间了,在博客园也默默的潜水了两个月,见识了很多大神,收获不少. 今天整理笔记,发现忘记的不少,我感觉需要及时的整理一下了,同时也把做的小东西贴上来和大家分享一下. 最近学习了Quar ...
- TD Rigging Demo Reel 性感美女绑定展示
161455520158189 这是一个充满回忆的Demo,非常怀念之前的工作生活,也特别感谢我长春的老哥张总对我的帮助与指导,不光是工作中,在生活上也让我有很大的收获.这个一直都觉得做的不够好,也从 ...
- 百度网盘kbengine - warring项目下载地址
http://pan.baidu.com/s/1k4J4y 下载解压之后,请看<<使用说明.doc>>,有更新指导
- css3 animation-fill-mode 对布局的影响
问题描述:在小米手机上,animation-fill-mode设置为 both时,在手机上的web页面会超出屏幕宽度,出现滚动条. 解决方法:animation-fill-mode设为none. .p ...
- MAC的终端命令
今天小研究了一下MAC的终端命令,主要为了方便调试程序用,XCODE用不来啊... 在这里记下..防止丢失 pwd 当前工作目录 cd(不加参数) 进root cd(folder) 进入文件夹 cd ...
- Oracle学习笔记(二)
2.Oracle用户管理 (1)创建用户:create user 用户名 identified by 密码(需要dba权限); sql>create user yzw identified by ...
- BarTender如何将条码下的数字嵌入到条码中
现今社会,在各种包装箱子.书籍.超市商品等东西上面,必不可少的绝对要数条形码或者二维码了.有时候,根据客户的需求或者其他条件限制等原因,我们需要将BarTender 2016条码下的数字嵌入到条码中. ...
- xmind8
win10企业版安装 xmind.zip,打开应用程序报下面的错 解决办法是 先备份xmind8下的XMind.ini文件 Open up Xmind.ini and replace "Ap ...
- Fatal error 829---数据库 ID 8,页 (1:80740) 已标记为 RestorePending,可能表明磁盘已损坏(日志备份和热备、双机的重要性)
问题现象: 在业务数据库中查询:SELECT a.NAME FROM SYSOBJECTS a WHERE a.NAME LIKE '%2015' AND a.XTYPE='u' 提示:消息 21,级 ...