77

Sorry for my inpatience but after weeks staying up late and just having put my web online, I just don't have any left energy to debug... I just can't Google how to implement JSON on IE6 & IE7... I'm using

JSON.stringify(...)

From what I understand JSON is not built in on IE6-7 and has to be dynamically added in in-line code... how do you do that?

I already have jQuery - is it my correct understanding that their JSON engine relies on the browser native one?

Then some comment on invalid JSON code that makes IE6-7 fail, but I thought it wasn't native in IE6-7?

Anyone?

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
David Thorisson
  • 1,033
  • 1
  • 11
  • 18

4 Answers4

105

Since you want to use the JSON.stringify method, you will need to include the JSON3 parser in order to support it on IE < 8.

This library complies with the standard methods of the ECMAScript 5th Edition specification and it checks if there is a native implementation available, so on modern browser this native implementation will be used.

Matty J
  • 3,116
  • 32
  • 31
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
2

Just include json2.js in your file to play around with JSON. It'll also work in IE 9 .

Padhu
  • 21
  • 3
2

There must be something misunderstood. The object notation has been in JavaScript for a while now (as far as I understand, it's a core concept of JavaScript). I mean, the ability to write code like var o= {a:"b"};
So, if you can do this, you can also call eval('var o= {a:"b"};') and that's the way you "implement JSON" in any browser.

UPDATE: Re-read your post and finally got the point that the problem is serializing objects, not deserializing them. Then you can use the JavaScript library for that: https://github.com/douglascrockford/JSON-js/blob/master/json2.js

naivists
  • 32,681
  • 5
  • 61
  • 85
  • @David, I think that's the only option you have if you receive JSON data from some external datasource. Isn't it? – naivists Mar 23 '10 at 20:11
  • Eval is the worst option if you want to load data from an external data source, it opens you up to script injection attacks. If you need `JSON` object compatibility json2.js is the way to go. – mikerobi Apr 18 '13 at 18:09
  • mikerobi, thanks for the comment (but not for the downvote). Be warned that json2.js calls eval internally as well (see https://github.com/douglascrockford/JSON-js/blob/master/json2.js#L471 ) Even though Crockford states that it is garded against execution, I still believe there may be a way to get around it. – naivists Apr 19 '13 at 05:57
1

"dynamically added in-line code" is using the functionality provided by Douglas Crockfords json2 library, or jQuery's own implementation if the browser version doesn't support it natively.

jQuery does not rely on any JSON decoding functionality provided by the browser. If the browser does support JSON decoding, then jQuery will use it.

Matt
  • 74,352
  • 26
  • 153
  • 180
  • ok using jQuery would be great since it's already included, but why then doesn't JSON.stringify(...) work on IE6-7, is there some special syntax for jQuery JSON? – David Thorisson Mar 23 '10 at 20:17
  • JSON.stringify does work on IE6-7, provided you have added the script :\ – Matt Mar 24 '10 at 01:18
  • In IE7, I get: SCRIPT5009: 'JSON' is undefined jquery-1.8.0.min.js, line 2 character 16953 when using this approach and not including the JSON-library. – Anders Branderud Jan 07 '13 at 12:01
  • @AndersBranderud: Should not be the case. jQuery explicitly provides a fallback. If you can reproduce this behavior in the latest version of jQuery (1.9.0b), I'd consider [filing a bug](http://bugs.jquery.com/). – Matt Jan 07 '13 at 12:06
  • @Matt , Thanks! I filed a bug report: With Jquery 1.9.0 and also jquery-1.8.0.min.js: A call of the method 'jQuery.parseJSON' causes: "SCRIPT5009: 'JSON' is undefined jquery-1.8.0.min.js, line 2 character 16953" in I.E. 7 (I use I.E.9 with IE7 as 'document version'). – Anders Branderud Jan 07 '13 at 14:17