JSON.stringify() 数组与 Prototype.js 的奇怪之处 [英] JSON.stringify() array bizarreness with Prototype.js

查看:39
本文介绍了JSON.stringify() 数组与 Prototype.js 的奇怪之处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出我的 json 序列化出了什么问题,将我的应用程序的当前版本与旧版本一起使用,并且我发现 JSON.stringify() 的工作方式存在一些令人惊讶的差异(使用来自的 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).

在我的旧版应用中:

 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 的 toJSON.然后,您将检查 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;
}

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

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