与Prototype.js的JSON.stringify()数组奇异性 [英] JSON.stringify() array bizarreness with Prototype.js

查看:134
本文介绍了与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;
}

这篇关于与Prototype.js的JSON.stringify()数组奇异性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆