JSON object undefined in Internet Explorer 8

JavascriptHtmlJsonInternet Explorer-8

Javascript Problem Overview


Currently I'm writing a JavaScript file and have the following line:

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?

Javascript Solutions


Solution 1 - Javascript

Make sure you're actually in IE 8 mode by using the preferred method, a standards doctype...

<!DOCTYPE html>

...or the undesired method, the X-UA-Compatible meta tag/header...

<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />

See Defining Document Compatibility for more information.

Solution 2 - Javascript

Using jQuery.parseJSON solved this for me, in case you are already using JQuery.

Solution 3 - Javascript

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.

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.

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.

Solution 4 - Javascript

function parseJson(jsonString) {
    if ($.browser.msie && $.browser.version < 8) {
        return eval('(' + jsonString + ')');
    }
    else {
        return JSON.parse(jsonString);
    }
}

Solution 5 - Javascript

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.

Solution 6 - Javascript

put following code in your js file ;

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;
 };

Solution 7 - Javascript

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.

<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.

if not working check this article.

Solution 8 - Javascript

In my case the undefined error was because I was missing a JSON library.

You can add JSON object like this (replace the relative path with your own path):

<script>
        if (typeof window.JSON == 'undefined') {
          document.write('<script src="../scripts/json2.js"><\/script>'); 
        }
</script>

For json2 library: http://cdnjs.com/libraries/json2/

There is also a json3 library: 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));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkeyboredView Question on Stackoverflow
Solution 1 - JavascriptAndy EView Answer on Stackoverflow
Solution 2 - JavascriptChenView Answer on Stackoverflow
Solution 3 - JavascriptThadeu de PaulaView Answer on Stackoverflow
Solution 4 - Javascriptmq3wView Answer on Stackoverflow
Solution 5 - Javascriptuser126083View Answer on Stackoverflow
Solution 6 - Javascriptatom217View Answer on Stackoverflow
Solution 7 - Javascriptsaeid mohammad hashemView Answer on Stackoverflow
Solution 8 - Javascriptlive-loveView Answer on Stackoverflow