简体   繁体   English

仅javascript中的jQuery.parseJSON函数(无jQuery)

[英]`jQuery.parseJSON` function in only javascript (without jQuery)

I was looking for the method to do jQuery.parseJSON in JavaScript that parses a json to return a JavaScript object. 我一直在寻找在JavaScript中执行jQuery.parseJSON的方法,该方法可解析json以返回JavaScript对象。 I can't use jQuery since the whole plugin that I have built is standalone JS and no jQuery has been used till now. 我无法使用jQuery,因为我构建的整个插件都是独立的JS,并且到目前为止还没有使用过jQuery。 Is there something of this sort that's already provided in JavaScript? JavaScript中是否已经提供了这类内容?

Use the native JSON object (this is the only time it is correct to say "JSON object", it is literally an object named JSON ) to manipulate JSON strings. 使用本机JSON对象(这是唯一一次正确地说“ JSON对象”,实际上是一个名为JSON的对象)来操纵JSON字符串。

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON https://developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Global_Objects/JSON

Use JSON.parse(yourJSONString); 使用JSON.parse(yourJSONString); to serialize and JSON.stringify(yourJSONObject); 序列化和JSON.stringify(yourJSONObject); to deserialize. 反序列化。

If you look at the jQuery core source on line 492, jQuery.parseJSON is just an alias to JSON.parse . 如果您查看第492行的jQuery核心源代码 ,则jQuery.parseJSON只是JSON.parse的别名。

You can either use the native JSON object , which is supported in most browsers, but you will run into an issue when you try to use it in the dinosaur browsers such as IE7 and lower. 您可以使用大多数浏览器都支持的本机JSON对象 ,但是尝试在IE7及更低版本的恐龙浏览器中使用它时会遇到问题。 There is the option to include a standalone plugin that mimics native functionality here (JSON.js). 可以选择在此处包含一个模仿本机功能的独立插件(JSON.js)。

Short answer: 简短答案:

Use the browser native method JSON.parse() 使用浏览器本机方法JSON.parse()

window.JSON.parse(jsonString);

Long answer: 长答案:

To get it working in old browsers, you can take the source code of jQuery.parseJSON and remove any dependencies on jQuery itself. 要使其在旧的浏览器中运行,您可以使用jQuery.parseJSON源代码并删除对jQuery本身的任何依赖关系。 Here is a working standalone version: 这是一个有效的独立版本:

function standaloneParseJson ( 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;
    }

    var rvalidchars = /^[\],:{}\s]*$/;
    var rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g;
    var rvalidescape = /\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g;
    var rvalidtokens = /"[^"\\\r\n]*"|true|false|null|-?(?:\d+\.|)\d+(?:[eE][+-]?\d+|)/g;

    if ( typeof data === "string" ) {

        // Make sure leading/trailing whitespace is removed (IE can't handle it)
        data = data.replace(/^\s+|\s+$/g, '');

        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 ) )();
            }
        }
    }

    // Error code here
    //jQuery.error( "Invalid JSON: " + data );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM