JSON object undefined in Internet Explorer 8

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?

Answers


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.


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


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.


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

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.


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

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.


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

Need Your Help

Controller specific layout in ZendFramework 2

php zend-framework2

I have a module in my zendframework 2 application which contains two controllers.

Increase storage on running EC2 using EBS

mysql amazon-web-services amazon-ec2 amazon-ebs

I have a instance running on EC2 with default storage of 8GB. I also have mysql running on that instance which is taking most of the storage on the instance.