Is JSON.stringify () supported in IE 8?

I need to use:

JSON.stringify() 

which should be supported by Chrome, Safari, and Firefox. I think IE8 also supports JSON object. I think IE7 and 6 do not do this, so I do this:

 <!--[if lt IE 8]> <script src="http://www.json.org/json2.js"></script> <![endif]--> 

so I think this only imports external JavaScript if IE6 and 7. I looked at the URL where the script is located, they are only enabled if the version of IE is less than 9:

 http://code.google.com/p/html5shiv/ <!--[if lt IE 9]> <script src="http://www.json.org/json2.js"></script> <![endif]--> 

so should i enable this for IE 8 too?

+56
javascript
Jul 24 '10 at 20:59
source share
7 answers

To answer the question in the header directly, yes, IE8 supports JSON.stringify() natively.

IE8 is the first version of IE to receive this support, and the functionality is explained in detail by the development team: http://blogs.msdn.com/b/ie/archive/2008/09/10/native-json-in-ie8.aspx

The answer to the second part of the question: yes, you will need to enable alternative functions for IE6 / IE7. Something like Modernizr makes it easy to verify this.

Also note that if the user is in compatibility mode in IE8, the JSON object will not be available.

+65
Aug 25 2018-11-18T00:
source share

If you try to use JSON.stringify() using IE 8, you need to make sure that it does not work in compatibility mode. See JSON object undefined in Internet Explorer 8

You need to add

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

to the page

+31
Jun 07 '12 at 22:15
source share

There is a better solution ...

This does not give a direct answer to your question, but provides a complete solution to your problem.

The jquery-json library provides a wrapper that uses the built-in implementation of the JSON object if it is available and crashes back to its own JSON implementation if it is not. This will work in any browser.

Here is a usage example on the project home page:

 var thing = {plugin: 'jquery-json', version: 2.3}; var encoded = $.toJSON( thing ); // '{"plugin":"jquery-json","version":2.3}' var name = $.evalJSON( encoded ).plugin; // "jquery-json" var version = $.evalJSON(encoded).version; // 2.3 

Usage is very simple: toJSON builds a JS source; evalJSON converts JSON string data back to JavaScript objects.

You look at the source, the implementation is surprisingly simple, but it works very well. I used it personally in several projects.

There is no need to perform browser detection if it works in every browser.

+16
Oct. 16
source share

You do not need to use conventions to determine if json2.js should be included or not. Take a look at the source code:

 var JSON; if (!JSON) { JSON = {}; } if (typeof JSON.stringify !== 'function') { JSON.stringify = function (value, replacer, space) { // Code } } if (typeof JSON.parse !== 'function') { JSON.parse = function (text, reviver) { // Code } } 

What does this mean, first check if JSON exists as an object. If not, it creates a new object to host the JSON functions. Then it checks if the native implementation of .stringify() or .parse() . If not, then it creates these functions.

Bottom line: if there is a built-in implementation, including json2.js will not overwrite the built-in implementation. Otherwise, it will add this functionality, so there is no reason why you need to use conventions if you are not trying to minimize queries.

(You can also note that IE10 does not support conditional statements, so I would recommend not relying on them if there is no alternative.)

+9
Aug 15 '12 at 19:08
source share

put the 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; }; 
+8
Feb 27 '14 at 13:09
source share

Only shiv only createElement HTML5 elements. This has nothing to do with JSON. Try to get the actual JSON parser, for example json2.js from Crockford.

+3
Jul 24 2018-10-24T00:
source share

Just to ensure that Mozilla creates a polyfill object for the JSON object, if you need it to work in IE compatibility mode.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON

 if (!window.JSON) { window.JSON = { parse: function(sJSON) { return eval('(' + sJSON + ')'); }, stringify: (function () { var toString = Object.prototype.toString; var isArray = Array.isArray || function (a) { return toString.call(a) === '[object Array]'; }; var escMap = {'"': '\\"', '\\': '\\\\', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t'}; var escFunc = function (m) { return escMap[m] || '\\u' + (m.charCodeAt(0) + 0x10000).toString(16).substr(1); }; var escRE = /[\\"\u0000-\u001F\u2028\u2029]/g; return function stringify(value) { if (value == null) { return 'null'; } else if (typeof value === 'number') { return isFinite(value) ? value.toString() : 'null'; } else if (typeof value === 'boolean') { return value.toString(); } else if (typeof value === 'object') { if (typeof value.toJSON === 'function') { return stringify(value.toJSON()); } else if (isArray(value)) { var res = '['; for (var i = 0; i < value.length; i++) res += (i ? ', ' : '') + stringify(value[i]); return res + ']'; } else if (toString.call(value) === '[object Object]') { var tmp = []; for (var k in value) { if (value.hasOwnProperty(k)) tmp.push(stringify(k) + ': ' + stringify(value[k])); } return '{' + tmp.join(', ') + '}'; } } return '"' + value.toString().replace(escRE, escFunc) + '"'; }; })() }; } 
+1
Jan 08 '16 at 16:00
source share



All Articles