首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >json.js和json2.js的区别

json.js和json2.js的区别
EN

Stack Overflow用户
提问于 2009-02-16 03:15:35
回答 3查看 81.3K关注 0票数 89

有人能告诉我这两个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等方法)。我在任何新版本中都没有看到这些方法。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-02-16 03:30:06

从他们的代码中:

代码语言:javascript
复制
// 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,那么只需将这段代码添加到某个地方,让它再次工作即可:

代码语言:javascript
复制
    Object.prototype.parseJSON = function (filter) {
        return JSON.parse(this, filter);
    };
票数 59
EN

Stack Overflow用户

发布于 2009-02-16 03:30:22

引用here

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

票数 31
EN

Stack Overflow用户

发布于 2012-06-01 02:20:07

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

在json2007中:

代码语言:javascript
复制
var array = [];
array[1] = "apple";
array[2] = "orange";
alert(array.toJSONString()); // Output: ["apple", "orange"].

在json2中:

代码语言:javascript
复制
var array = [];
array[1] = "apple";
array[2] = "orange";
alert(JSON.stringify(array)); // Output: [null, "apple", "orange"].
票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/552135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档