JSON.stringify() array bizarreness with Prototype.js(JSON.stringify() 数组的怪异与 Prototype.js)
问题描述
我正在尝试找出我的 json 序列化出了什么问题,将我的应用程序的当前版本与旧版本一起使用,并发现 JSON.stringify() 的工作方式存在一些令人惊讶的差异(使用来自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 的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JSON.stringify() 数组的怪异与 Prototype.js


- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01