jQuery.parseJSON vs JSON.parse

编程入门 行业动态 更新时间:2024-10-27 00:25:37
本文介绍了jQuery.parseJSON vs JSON.parse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

jQuery.parseJSON 和 JSON.parse 两个执行相同任务的函数。如果已加载jQuery库,则使用 jQuery.parseJSON 比使用 JSON.parse 更好性能?

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?

如果是,为什么?如果不是,为什么不呢?

If yes, why? If no, why not?

推荐答案

这是摘录来自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 json/json2.js if ( rvalidchars.test( data.replace( rvalidescape, "@" ) .replace( rvalidtokens, "]" ) .replace( rvalidbraces, "")) ) { return ( new Function( "return " + data ) )(); } } } jQuery.error( "Invalid JSON: " + data ); },

如你所见,jQuery将使用原生 JSON.parse 方法(如果可用),否则它将尝试使用 new Function 评估数据,这有点像 eval 。

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.parseJSON 。

So yes, you should definitely use jQuery.parseJSON.

更多推荐

jQuery.parseJSON vs JSON.parse

本文发布于:2023-11-07 06:52:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565815.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:parseJSON   jQuery   parse   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!