json - json.js 和 json2.js 的区别

谁能告诉我这两个 JSON 解析器有什么区别?

https://github.com/douglascrockford/JSON-js/blob/master/json.js
https://github.com/douglascrockford/JSON-js/blob/master/json2.js

我有一个 2007-04-13 的 JSON 文件(它有诸如 parseJSON 之类的方法)。我在任何新版本中都没有看到这些方法。

最佳答案

来自他们的代码:

// Augment the basic prototypes if they have not already been augmented.
// These forms are obsolete. It is recommended that JSON.stringify and
// JSON.parse be used instead.

if (!Object.prototype.toJSONString) {
    Object.prototype.toJSONString = function (filter) {
        return JSON.stringify(this, filter);
    };
    Object.prototype.parseJSON = function (filter) {
        return JSON.parse(this, filter);
    };
}

我猜 parseJSON 已经过时了,因此新版本 (json2) 甚至不再使用它了。但是,如果您的代码使用 parseJSON 很多,您可以在某处添加这段代码以使其再次工作:

    Object.prototype.parseJSON = function (filter) {
        return JSON.parse(this, filter);
    };

https://stackoverflow.com/questions/552135/

相关文章:

sql - 查询 JSON 类型内的数组元素

javascript - 未捕获的类型错误 : Cannot use 'in' operator t

python - 如何在单元测试中使用 JSON 发送请求

json - 在 VS Code 中,禁用错误 "Comments are not permitte

jquery - 为什么找不到我的 json 文件?

c# - 解析值时遇到意外字符

json - 如何使用 jq 从 JSON 中获取键名

c# - DataContractJsonSerializer 和 JavaScriptSerial

python - 如何在 Python 中美化 JSON?

json - AngularJS:工厂 $http.get JSON 文件