JSON.stringify and JSON.parse not working in IE9?
Asked Answered
A

4

19

I'm using JSON.Stringify and JSON.parse everywhere and it works fine with Firefox. It's working no more with IE9 nor does it work in IE8. What can I do?

Attribution answered 22/8, 2011 at 10:41 Comment(3)
Can you paste some code?Cure
see more here how make it with IE 8 #3327393Levator
see more here #3327393Levator
P
11

why do you want to depend on the browser having the object instead just include the script file by Douglas Crockford.. You can find the minifed file here: http://www.json.org/js.html

Once imported you dont have to worry abt the method existing in a browser.

Paramecium answered 22/8, 2011 at 14:31 Comment(6)
Looks better idea to me but which file i should add. There are a lot of files github.com/douglascrockford/JSON-jsAttribution
Use json2.js. That's the latest one.. Get the minified 1 if u canParamecium
Nothing special just a file with no spaces returns and minified variable names etc to reduce the file size.. Google bat us minification and u will find what it is.. If thud minified file isnt there pickup json2.js and u can think of minifying it if u want laterParamecium
When I started using JSON.stringify I was surprised to find it built into the browser (Firefox) even though I was sure it wouldn't work in IE8. I forgot to test in IE8 and sure enough, my app didn't work once I tried it there. Thanks for the helpful solution!Bomarc
I would suggest to load json.org conditionally only if browser does not support JSON object navively by checking if window.JSON is defined. Otherwise you increase an number of downloaded JS filesWyler
Might the json2.js run slower than what is natively supported in IE10 as well? (another reason to conditionally load it, rather than always loading?)Mistress
S
35

JSON.stringify starts with a lower-case s. Both stringify and parse are available in IE8+, but only in standards mode.

Prepend your document with <!DOCTYPE html> if you're currently using quirks mode. Also, watch the capitalization of the JavaScript methods you call - all built-in ones start with a lower-case character.

Starchy answered 22/8, 2011 at 10:54 Comment(2)
In addition, watch out if you're developing locally against a localhost address. A Windows Update to IE9 a few weeks ago caused it to begin automatically using compatibility mode for some localhost addresses, which can result in JSON.parse/stringify suddenly not being available even when your markup shouldn't have triggered quirks mode.Sealed
i have something already like <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">Attribution
P
11

why do you want to depend on the browser having the object instead just include the script file by Douglas Crockford.. You can find the minifed file here: http://www.json.org/js.html

Once imported you dont have to worry abt the method existing in a browser.

Paramecium answered 22/8, 2011 at 14:31 Comment(6)
Looks better idea to me but which file i should add. There are a lot of files github.com/douglascrockford/JSON-jsAttribution
Use json2.js. That's the latest one.. Get the minified 1 if u canParamecium
Nothing special just a file with no spaces returns and minified variable names etc to reduce the file size.. Google bat us minification and u will find what it is.. If thud minified file isnt there pickup json2.js and u can think of minifying it if u want laterParamecium
When I started using JSON.stringify I was surprised to find it built into the browser (Firefox) even though I was sure it wouldn't work in IE8. I forgot to test in IE8 and sure enough, my app didn't work once I tried it there. Thanks for the helpful solution!Bomarc
I would suggest to load json.org conditionally only if browser does not support JSON object navively by checking if window.JSON is defined. Otherwise you increase an number of downloaded JS filesWyler
Might the json2.js run slower than what is natively supported in IE10 as well? (another reason to conditionally load it, rather than always loading?)Mistress
T
0

For an alternative, in a scenario where you might need to run in strict mode for whatever reason (I have another library that includes "use strict"), you can look here: https://github.com/douglascrockford/JSON-js. I modified this to check first if JSON is undefined, and only generate the function JSON.parse if it is:

if (typeof JSON === "undefined") {
    var JSON = {
        parse: <insert value of json_parse from library here>
    };
}

My issue was application code not working in IE9 (strict mode being used by a participating library, I believe). That solved the problem for me.

Teresitateressa answered 20/6, 2015 at 12:28 Comment(0)
P
0

the mere issue is, that sending UTF-8 headers will invalidate the JSON (IE doesn't/didn't like that). as the issue is described, that might still apply for IE9... once wrote a how to, a few years ago. adding JSON support to a browser which can parse native JSON is probably not the optimal solution, since it produces useless overhead - only because failing to deliver the JSON in the expected format.

Pearlene answered 21/6, 2015 at 1:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.