有人能告诉我这两个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
等方法)。我在任何新版本中都没有看到这些方法。
发布于 2009-02-16 03:30:06
从他们的代码中:
// 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);
};
发布于 2009-02-16 03:30:22
引用here
"JSON2.js -去年年底,Crockford悄悄发布了他的JSON API的新版本,取代了他现有的API。重要的区别是它使用了一个单一的基础对象。“
发布于 2012-06-01 02:20:07
我还注意到json2对数组的字符串化方式与json2007不同。
在json2007中:
var array = [];
array[1] = "apple";
array[2] = "orange";
alert(array.toJSONString()); // Output: ["apple", "orange"].
在json2中:
var array = [];
array[1] = "apple";
array[2] = "orange";
alert(JSON.stringify(array)); // Output: [null, "apple", "orange"].
https://stackoverflow.com/questions/552135
复制相似问题