IE6中的JSON对象 - 如何? [英] JSON object in IE6 - How?

查看:78
本文介绍了IE6中的JSON对象 - 如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速问题可能对于知道某人而言是一块蛋糕。

Quick questions that probably a piece of cake for someone in the know to asnwer.

我有一个简单的asp.net网站,它使用JSON作为一堆东西(和JSON.stringify)

I have a simple asp.net website that uses JSON for a bunch of stuff (and JSON.stringify)

所有在firefox等方面都不错,但在IE6中我遇到了一个错误,JSON未定义。

All good in firefox etc, yet, in IE6 I run into an error with JSON being undefined.

有没有一种方法可以包含JSON实现而不会破坏我已经拥有的东西(使用其他浏览器中的本机JSON对象)。
如果是这样,怎么样?

Is there a way I can include a JSON implementation without breaking what I have already (using the native JSON objects in the other browsers). If so, how?

谢谢!

推荐答案

https://github.com/douglascrockford/JSON-js 上的json2库正是如此您正在寻找。您可以无条件地包含它,并将JSON.parse和JSON.stringify添加到您的全局命名空间(仅当尚未定义时)。它不会破坏任何内置的JSON。从其来源:

The json2 library at https://github.com/douglascrockford/JSON-js is exactly what you're looking for. You can include it unconditionally, and it adds JSON.parse and JSON.stringify to your global namespace (only if there isn't one defined yet). It won't mess with any built-in JSON. From its source:

if (!this.JSON) {
    this.JSON = {};
}
...
if (typeof JSON.stringify !== 'function') {
...
if (typeof JSON.parse !== 'function') {

这是全面的!即使由于某种原因你已经拥有JSON.stringify而不是JSON.parse(反之亦然)它仍然会做正确的事情,留下原来的。

That's comprehensive! Even if for some reason you already have JSON.stringify but not JSON.parse (or vice versa) it'll still do the right thing, leaving the original ones in place.

这篇关于IE6中的JSON对象 - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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