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
JSONobject for a description of JSON syntax. reviverOptional- 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 ...
随机推荐
- Tomcat Start 报错 (COULD NOT DELETE MAY BE LOCKED BY ANOTHER PROCESS)
jsp文件重命名后发布不起来了,提示文件被占用,原因是当前的java ee项目 与它引用的java项目 依赖了相同的jar包,删除了clean 再发布,问题解决,如有需要再引用回来 http://it ...
- Vim找不到配色文件的解决方法
Vim新出了8.0,又成功的勾起了我的好奇心. 重新从零开始配置,结果第一步设置配色主题就没过,好丢人-- 提示找不到evening.vim配色文件,于是上网查了一下,有说改环境变量的,又说改这个改那 ...
- 数位DP之奥义
恩是的没错数位DP的奥义就是一个简练的dfs模板 int dfs(int position, int condition, bool boundary) { ) return (condition ? ...
- 微信网页授权snsapi_base、snsapi_userinfo的问题
微信网页授权SCOPE分为snsapi_base.snsapi_userinfo,前者是用户无感知的静默授权只能拿到openid:而后者需要用户确认,能拿到更多的用户信息. 我有一个系统需要进行网页授 ...
- 控制HTML Input只能输入数字和小数点
本文介绍两种控制在<input type="text" name="name" value="0" />中只允许输入数字和小数点 ...
- linux下shell编写九九乘法表
主要语法:类似 1x2 echo $((1*2)) for 变量 in 值1 值2 值3 ;do linux命令或者语句done
- MS-MPI 的使用
MPI在windows上的扯淡 MPI的实现一般使用MPICH与OpenMPI,这两个库在12年的版本就已经停止了对windows的更新,不支持MPI的新特性(也不知道有没有bug方面的问题),配置的 ...
- [原]JQuery mobile CSS 文件组织
从 JQuery mobile 1.4 开始, CSS 由3个部分组成,分别是 Icons.Theme和Structure jquery.mobile-1.4.x.css: 包括了 <标准图标 ...
- 动态linq to list排序
public class QeurySort { public static IList<T> Sort<T>(IList<T> list,string sidx, ...
- some notes about spring aop
1 . timeCountIntecetor implements handlerInterceptor { preHandle(); postHandle(); afterComplete(); } ...