json.js 和 json2.js 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/552135/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:11:11  来源:igfitidea点击:

Difference between json.js and json2.js

json

提问by

Can someone tell me what the difference is between the 2 JSON parsers?

有人能告诉我 2 个 JSON 解析器之间有什么区别吗?

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

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

I have a JSON file from 2007-04-13 (It has methods such as parseJSON). I don't see these methods in any of the new versions.

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

采纳答案by Luca Matteis

From their code:

从他们的代码:

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

I guess parseJSON is obsolete, therefore the new version (json2) doesn't even use it anymore. However if your code uses parseJSONa lot you could just add this piece of code somewhere to make it work again:

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

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

回答by paxdiablo

Quoting here:

引用这里

"JSON2.js - Late last year Crockford quietly released a new version of his JSON API that replaced his existing API. The important difference was that it used a single base object."

“JSON2.js - 去年年底,Crockford 悄悄发布了他的 JSON API 的新版本,取代了他现有的 API。重要的区别是它使用了一个基础对象。”

回答by Vimil Saju

I also noticed that json2 stringified arrays differently than json2007.

我还注意到 json2 字符串化数组与 json2007 不同。

In json2007:

在 json2007 中:

var array = [];
array[1] = "apple";
array[2] = "orange";
alert(array.toJSONString()); // Output: ["apple", "orange"].

In json2:

在 json2 中:

var array = [];
array[1] = "apple";
array[2] = "orange";
alert(JSON.stringify(array)); // Output: [null, "apple", "orange"].