Javascript jQuery.parseJSON 与 JSON.parse
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10362277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery.parseJSON vs JSON.parse
提问by Question Overflow
jQuery.parseJSON
and JSON.parse
are two functions that perform the same task. If the jQuery library is already loaded, would using jQuery.parseJSON
be better than using JSON.parse
, in terms of performance?
jQuery.parseJSON
和JSON.parse
是执行相同任务的两个函数。如果已经加载了 jQuery 库,就性能而言,使用jQuery.parseJSON
会比使用更好JSON.parse
吗?
If yes, why? If no, why not?
如果是,为什么?如果没有,为什么不呢?
回答by dfsq
Here is an extract from jQuery 1.9.1:
这是jQuery 1.9.1的摘录:
parseJSON: function( data ) {
// Attempt to parse using the native JSON parser first
if ( window.JSON && window.JSON.parse ) {
return window.JSON.parse( data );
}
if ( data === null ) {
return data;
}
if ( typeof data === "string" ) {
// Make sure leading/trailing whitespace is removed (IE can't handle it)
data = jQuery.trim( data );
if ( data ) {
// Make sure the incoming data is actual JSON
// Logic borrowed from http://json.org/json2.js
if ( rvalidchars.test( data.replace( rvalidescape, "@" )
.replace( rvalidtokens, "]" )
.replace( rvalidbraces, "")) ) {
return ( new Function( "return " + data ) )();
}
}
}
jQuery.error( "Invalid JSON: " + data );
},
As you can see, jQuery will use the native JSON.parse
method if it is available, and otherwise it will try to evaluate the data with new Function
, which is kind of like eval
.
如您所见,jQuery 将使用本机JSON.parse
方法(如果可用),否则它将尝试使用 来评估数据new Function
,这有点像eval
。
So yes, you should definitely use jQuery.parseJSON
.
所以是的,你绝对应该使用jQuery.parseJSON
.
回答by Joseph
Where the browser provides a native implementation of JSON.parse, jQuery uses it to parse the string.
浏览器提供 JSON.parse 的本机实现,jQuery 使用它来解析字符串。
thus it means that jQuery provides a JSON parser if no native implementation exists on the browser. here's a comparison chartof browsers that have (and don't have) JSON functionality
因此,这意味着如果浏览器上不存在本机实现,则 jQuery 会提供 JSON 解析器。这是具有(和不具有)JSON 功能的浏览器的比较图
回答by leifbennett
JSON.parse() is natively available on some browsers, not on others, so it's safer to use a library. The JQuery implementation works well, as other respondents have noted. There's also Douglas Crockford's JSON library, which uses the native implementation if available.
JSON.parse() 在某些浏览器上本机可用,在其他浏览器上不可用,因此使用库更安全。正如其他受访者所指出的,JQuery 实现运行良好。还有Douglas Crockford 的 JSON 库,它使用本机实现(如果可用)。
The JSON library has the advantage that it has a method to turn a JavaScript object into a JSON string, which is missing from jQuery at the moment..
JSON 库的优点是它有一种方法可以将 JavaScript 对象转换为 JSON 字符串,而目前 jQuery 缺少这种方法。
回答by Khuram Khan
If you're using jQuery version 3 (released in 2016) then you should use JSON.parse()
because jQuery.parseJSON()
has been deprecated.
如果您使用的是 jQuery 版本 3(2016 年发布),那么您应该使用JSON.parse()
因为jQuery.parseJSON()
已被弃用。
As of jQuery 3.0, $.parseJSON is deprecated. To parse JSON objects, use the native JSON.parse method instead.
从 jQuery 3.0 开始,不推荐使用 $.parseJSON。要解析 JSON 对象,请改用原生 JSON.parse 方法。
回答by gion_13
I don't know about performance, but it's definitely safer to use the jQuery method because some browsers like ie7 and lower might not have any JSON functionalities natively.
It's all about compatibility, just like you use jQuery's each method instead of the array's native forEach
method for iteration.
我不知道性能,但使用 jQuery 方法肯定更安全,因为某些浏览器(如 ie7 及更低版本)可能本身没有任何 JSON 功能。
这完全是为了兼容性,就像您使用 jQuery 的 each 方法而不是数组的本机forEach
方法进行迭代一样。
回答by giovannipds
Talking about performance, the most updatedanswer is JSON.parse
.
谈到性能,最新的答案是JSON.parse
。
The native JSON object is supportedin every browsernowadays, so opt for JSON.parse
. You can see the support table here: http://caniuse.com/#feat=json
原生JSON对象支持在每一个浏览器如今,这样的选择JSON.parse
。您可以在此处查看支持表:http: //caniuse.com/#feat=json
You can also search for this alias appearances at JQuery's repository on GitHub: https://github.com/jquery/jquery/search?utf8=%E2%9C%93&q=parseJSON
您还可以在 GitHub 上的 JQuery 存储库中搜索此别名外观:https: //github.com/jquery/jquery/search?utf8 =%E2%9C%93 &q =parseJSON
Also, jQuery.parseJson
was deprecatedon version 3.0+ as mentioned by other answers here.
此外,如此处其他答案所述,已在 3.0+ 版jQuery.parseJson
中弃用。
You should only use jQuery's version if you're an old JQuery version + if you want to provide support for very old browsers (normally, not recommended).
如果您是旧的 JQuery 版本,您应该只使用 jQuery 的版本 + 如果您想为非常旧的浏览器提供支持(通常,不推荐)。
回答by Siva Prakash
jQuery internally uses JSON.parse
to parse the JSON file.So it doesn't make any difference in most cases.
jQuery 内部用于JSON.parse
解析 JSON 文件。所以在大多数情况下它没有任何区别。
But some of the older browsers don't support JSON.parse
functionality.In that case using jQuery.parseJSON
is beneficial as jQuery can handle JSON using its own function.
但是一些较旧的浏览器不支持JSON.parse
功能。在这种情况下,使用jQuery.parseJSON
是有益的,因为 jQuery 可以使用自己的函数处理 JSON。
NOTE:
笔记:
jQuery.parseJSON
is deprecated from jQuery 3.0.So please use the nativeJSON.parse
method.
jQuery.parseJSON
jQuery 3.0 已弃用。所以请使用本机JSON.parse
方法。