Javascript JSON 对象在 Internet Explorer 8 中未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4715373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON object undefined in Internet Explorer 8
提问by keybored
Currently I'm writing a JavaScript file and have the following line:
目前我正在编写一个 JavaScript 文件并有以下几行:
var res = "JSON=" + JSON.stringify(result);
result is being set just above this line. The issue I'm having is that IE8 (IE8 only, that is) is reporting to me that JSON is undefined somehow. I'm not sure what to make of this since, as I understood it, IE8 is a browser that implemented JSON support. Does anyone have any idea what might be going on?
结果被设置在这条线的正上方。我遇到的问题是 IE8(仅限 IE8)向我报告 JSON 以某种方式未定义。我不知道该怎么做,因为据我所知,IE8 是一个实现了 JSON 支持的浏览器。有谁知道可能会发生什么?
回答by Andy E
Make sure you're actually in IE 8 mode by using the preferred method, a standards doctype...
通过使用首选方法,即标准文档类型,确保您实际上处于 IE 8 模式...
<!DOCTYPE html>
...or the undesired method, the X-UA-Compatible
meta tag/header...
...或不需要的方法,X-UA-Compatible
元标记/标题...
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
See Defining Document Compatibilityfor more information.
有关更多信息,请参阅定义文档兼容性。
回答by Chen
Using jQuery.parseJSONsolved this for me, in case you are already using JQuery.
使用jQuery.parseJSON为我解决了这个问题,以防您已经在使用 JQuery。
回答by Thadeu de Paula
Other things that absence of doctype or wrong doctype, or some error with html syntax, will force IE to use document modes different from what you expect.
其他缺少 doctype 或错误的 doctype,或者 html 语法错误,将迫使 IE 使用与您期望不同的文档模式。
I was using simple "" in a test document and the absence of TITLE tag as a child of HEAD tag made window.JSON become undefined.
我在测试文档中使用了简单的 "" 并且没有 TITLE 标记作为 HEAD 标记的子级使 window.JSON 变得未定义。
Remember always that it's better to test the resource against the version of browser. And, if your users can use IE's with emulation of document modes, it's better you have a piece of code to provide the JSON.parse and JSON.stringify when natives are undefined.
请始终记住,最好针对浏览器版本测试资源。而且,如果您的用户可以使用 IE 来模拟文档模式,那么您最好有一段代码来在未定义本机时提供 JSON.parse 和 JSON.stringify。
回答by mq3w
function parseJson(jsonString) {
if ($.browser.msie && $.browser.version < 8) {
return eval('(' + jsonString + ')');
}
else {
return JSON.parse(jsonString);
}
}
回答by user126083
May happen despite <!DOCTYPE html>
if the page encoding is UTF-8
with BOM
(byte order mark). Try saving the file as UTF-8
without BOM
, using a suitable text editor.
尽管<!DOCTYPE html>
页面编码UTF-8
带有BOM
(字节顺序标记),但仍可能发生。尝试使用合适的文本编辑器将文件另存为UTF-8
without BOM
。
回答by atom217
put following code in your js file ;
将以下代码放入您的 js 文件中;
var JSON = JSON || {};
// implement JSON.stringify serialization
JSON.stringify = JSON.stringify || function (obj) {
var t = typeof (obj);
if (t != "object" || obj === null) {
// simple data type
if (t == "string") obj = '"'+obj+'"';
return String(obj);
}
else {
// recurse array or object
var n, v, json = [], arr = (obj && obj.constructor == Array);
for (n in obj) {
v = obj[n]; t = typeof(v);
if (t == "string") v = '"'+v+'"';
else if (t == "object" && v !== null) v = JSON.stringify(v);
json.push((arr ? "" : '"' + n + '":') + String(v));
}
return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
}
};
// implement JSON.parse de-serialization
JSON.parse = JSON.parse || function (str) {
if (str === "") str = '""';
eval("var p=" + str + ";");
return p;
};
回答by live-love
In my case the undefined error was because I was missing a JSON library.
就我而言,未定义错误是因为我缺少 JSON 库。
You can add JSON object like this (replace the relative path with your own path):
您可以像这样添加 JSON 对象(用您自己的路径替换相对路径):
<script>
if (typeof window.JSON == 'undefined') {
document.write('<script src="../scripts/json2.js"><\/script>');
}
</script>
For json2 library: http://cdnjs.com/libraries/json2/
对于 json2 库:http://cdnjs.com/libraries/json2/
There is also a json3 library: http://cdnjs.com/libraries/json3/
还有一个json3库:http://cdnjs.com/libraries/json3/
Then you can refer to it in your code:
然后你可以在你的代码中引用它:
var array = [];
array[1] = "apple";
array[2] = "orange";
alert(JSON.stringify(array));
回答by saeid mohammad hashem
Check jQuery version. jQuery 2.0 drops support for IE 6, 7 and 8. Use jQuery 1.x instead, which is still officially supported. you can use this Code.
检查 jQuery 版本。 jQuery 2.0 不再支持 IE 6、7 和 8。请改用 jQuery 1.x,它仍受官方支持。您可以使用此代码。
<script src="http://code.jquery.com/jquery-1.9.0.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.js"></script>
read more about jquery migrate.
阅读更多关于jquery migrate 的信息。
if not working check this article.
如果不起作用,请查看这篇文章。