json.js和json2.js之间的区别

有人能告诉我两个JSONparsing器有什么不同吗?

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

在这里引用:

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

我也注意到json2string数组比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"].