与Prototype.js的JSON.stringify()数组奇异性

问题描述:

我正在试图弄清楚我的json序列化出了什么问题,将我的应用程序的当前版本与旧版本一起使用,并且在JSON.stringify()的工作方式中发现了一些令人惊讶的差异(使用来自JSON库的JSON库) json.org)。

I'm trying to figure out what's gone wrong with my json serializing, have the current version of my app with and old one and am finding some surprising differences in the way JSON.stringify() works (Using the JSON library from json.org).

在我的应用程序的旧版本中:

In the old version of my app:

 JSON.stringify({"a":[1,2]})

给了我这个;

"{\"a\":[1,2]}"

新版本,

 JSON.stringify({"a":[1,2]})

给我这个;

"{\"a\":\"[1, 2]\"}"

任何想法可以改变什么来使同一个库在新版本的数组括号中放置引号?

any idea what could have changed to make the same library put quotes around the array brackets in the new version?

由于JSON.stringify最近已经在某些浏览器中出货,我建议使用它而不是Prototype的toJS上。然后你会检查window.JSON&& window.JSON.stringify,否则只包含json.org库(通过 document.createElement('script') ...)。要解决不兼容问题,请使用:

Since JSON.stringify has been shipping with some browsers lately, I would suggest using it instead of Prototype’s toJSON. You would then check for window.JSON && window.JSON.stringify and only include the json.org library otherwise (via document.createElement('script')…). To resolve the incompatibilities, use:

if(window.Prototype) {
    delete Object.prototype.toJSON;
    delete Array.prototype.toJSON;
    delete Hash.prototype.toJSON;
    delete String.prototype.toJSON;
}