/* Copyright (c) 2005 JSON.orgPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:The Software shall be used for Good, not Evil.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *//* The global object JSON contains two methods. JSON.stringify(value) takes a JavaScript value and produces a JSON text. The value must not be cyclical. JSON.parse(text) takes a JSON text and produces a JavaScript value. It will return false if there is an error. */ var JSON = function () { var m = { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\' }, s = { 'boolean': function (x) { return String(x); }, number: function (x) { return isFinite(x) ? String(x) : 'null'; }, string: function (x) { if (/["\\\x00-\x1f]/.test(x)) { x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) { var c = m[b]; if (c) { return c; } c = b.charCodeAt(); return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); }); } return '"' + x + '"'; }, object: function (x) { if (x) { var a = [], b, f, i, l, v; if (x instanceof Array) { a[0] = '['; l = x.length; for (i = 0; i < l; i += 1) { v = x[i]; f = s[typeof v]; if (f) { v = f(v); if (typeof v == 'string') { if (b) { a[a.length] = ','; } a[a.length] = v; b = true; } } } a[a.length] = ']'; } else if (x instanceof Object) { a[0] = '{'; for (i in x) { v = x[i]; f = s[typeof v]; if (f) { v = f(v); if (typeof v == 'string') { if (b) { a[a.length] = ','; } a.push(s.string(i), ':', v); b = true; } } } a[a.length] = '}'; } else { return; } return a.join(''); } return 'null'; } }; return { copyright: '(c)2005 JSON.org', license: 'http://www.crockford.com/JSON/license.html', /* Stringify a JavaScript value, producing a JSON text. */ stringify: function (v) { var f = s[typeof v]; if (f) { v = f(v); if (typeof v == 'string') { return v; } } return null; }, /* Parse a JSON text, producing a JavaScript value. It returns false if there is a syntax error. */ parse: function (text) { try { return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test( text.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + text + ')'); } catch (e) { return false; } } }; }();
to wcwtitxu # (君当如狼)本地的静态页面,如何确定是否使用了兼容模式?还有不知你的测试结果如何。我发这个贴,是因为IE8说支持原生JSON,也就是可以直接使用JSON这个对象,在rc版本的IE8是能得到的,但我正式IE8中得不到这个对象,我不知究竟哪里有问题,麻烦XDJM也测试下。
to wcwtitxu # (君当如狼) 将浏览器模式和文本模式都调整为IE8模式,能够alert(JSON)对象出来,不会报对象不存在。但我的问题就来了,如果我一定要在“开发者工具”里面作这样的调整才能有该对象出来,那有何实际意义?因为普通用户都是使用默认安装的IE8,他不会去改这些设置,也不应该让用户去修改设置。而且我关掉IE8后,再打开IE8测试页面,仍然会报JSON不存在,也就是说在“开发者工具”里面作的修改只会对当前页面有效,它不会改变浏览器的全局设置。如果这样,我们现在敢用这个原生的JSON对象么?
<script type="text/javascript">
alert(接分);
</script>
自己试一下就知道咯var obj = {
'num':123
, 'str': 'abcde?>>"\'!@$@#%&\r\n中文ft'
, 'dat': new Date()
, 'reg': new RegExp('abc')
, ary:[1,2,3, 4,5,6]
, 'NaN': NaN
, 'null': null
, 'undefined':undefined
};
alert(JSON.stringify(obj));
<head>
<script language="JavaScript" type="text/javascript">
//language="JavaScript" 使用language是为了在Opera中alert有效
alert(navigator.userAgent)
alert(JSON);
</script>
</head>
<body>
</body>
</html>
Copyright (c) 2005 JSON.orgPermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The Software shall be used for Good, not Evil.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*//*
The global object JSON contains two methods. JSON.stringify(value) takes a JavaScript value and produces a JSON text.
The value must not be cyclical. JSON.parse(text) takes a JSON text and produces a JavaScript value. It will
return false if there is an error.
*/
var JSON = function () {
var m = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
},
s = {
'boolean': function (x) {
return String(x);
},
number: function (x) {
return isFinite(x) ? String(x) : 'null';
},
string: function (x) {
if (/["\\\x00-\x1f]/.test(x)) {
x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
var c = m[b];
if (c) {
return c;
}
c = b.charCodeAt();
return '\\u00' +
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
});
}
return '"' + x + '"';
},
object: function (x) {
if (x) {
var a = [], b, f, i, l, v;
if (x instanceof Array) {
a[0] = '[';
l = x.length;
for (i = 0; i < l; i += 1) {
v = x[i];
f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
}
a[a.length] = v;
b = true;
}
}
}
a[a.length] = ']';
} else if (x instanceof Object) {
a[0] = '{';
for (i in x) {
v = x[i];
f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
}
a.push(s.string(i), ':', v);
b = true;
}
}
}
a[a.length] = '}';
} else {
return;
}
return a.join('');
}
return 'null';
}
};
return {
copyright: '(c)2005 JSON.org',
license: 'http://www.crockford.com/JSON/license.html',
/*
Stringify a JavaScript value, producing a JSON text.
*/
stringify: function (v) {
var f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
return v;
}
}
return null;
},
/*
Parse a JSON text, producing a JavaScript value.
It returns false if there is a syntax error.
*/
parse: function (text) {
try {
return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
text.replace(/"(\\.|[^"\\])*"/g, ''))) &&
eval('(' + text + ')');
} catch (e) {
return false;
}
}
};
}();
<head>
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<body>
<script type="text/javascript">
alert(typeof JSON)
</script>
</body>
</html> 或者<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<body>
<script type="text/javascript">
alert(typeof JSON)
</script>
</body>
</html> 才可以使用
http://dotnet.aspx.cc/article/a36b0df9-d63e-423a-8a51-be827e5e62d1/read.aspx
http://blog.csdn.net/net_lover/archive/2010/01/06/5142499.aspx
对象检测无法做到的,才考虑浏览器检测。
既然许多浏览器版本不支持JSON,那么就需要检测,也就是typeof
楼主你不会是想做个只兼容IE8的网页来吧?
if (!this.JSON) {
this.JSON = {};
}
但在RC版本的IE8中,如果一个对象的属性值是空串的话,通过stringify方法转换后会把""空串变为"null"。这个应该是RC版本的bug,在正式IE8版本中,我测试发现没有这个问题了。