jQuery.parseJSON vs JSON.parse

后端 未结 7 1173
感情败类
感情败类 2020-11-27 06:29

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 th

相关标签:
7条回答
  • 2020-11-27 06:51

    If you're using jQuery version 3 (released in 2016) then you should use JSON.parse() because jQuery.parseJSON() has been deprecated.

    As of jQuery 3.0, $.parseJSON is deprecated. To parse JSON objects, use the native JSON.parse method instead.

    0 讨论(0)
  • 2020-11-27 06:55

    Talking about performance, the most updated answer is JSON.parse.

    The native JSON object is supported in every browser nowadays, so opt for JSON.parse. You can see the support table here: 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

    Also, jQuery.parseJson was deprecated on version 3.0+ as mentioned by other answers here.

    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).

    0 讨论(0)
  • 2020-11-27 06:56

    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.

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

    0 讨论(0)
  • 2020-11-27 06:59

    According to jQuery

    Where the browser provides a native implementation of JSON.parse, jQuery uses it to parse the string.

    thus it means that jQuery provides a JSON parser if no native implementation exists on the browser. here's a comparison chart of browsers that have (and don't have) JSON functionality

    0 讨论(0)
  • 2020-11-27 07:08

    Here is an extract from 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.

    So yes, you should definitely use jQuery.parseJSON.

    0 讨论(0)
  • 2020-11-27 07:11

    jQuery internally uses JSON.parse to parse the JSON file.So it doesn't make any difference in most cases.

    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.

    NOTE:

    jQuery.parseJSON is deprecated from jQuery 3.0.So please use the native JSON.parse method.

    0 讨论(0)
提交回复
热议问题