23

Quick questions that probably a piece of cake for someone in the know to asnwer.

I have a simple asp.net website that uses JSON for a bunch of stuff (and JSON.stringify)

All good in firefox etc, yet, in IE6 I run into an error with JSON being undefined.

Is there a way I can include a JSON implementation without breaking what I have already (using the native JSON objects in the other browsers). If so, how?

Thanks!

5
  • What is it you're using? json.org/json2.js ? Commented Nov 24, 2009 at 0:23
  • Or are you not using any library and expecting JSON.stringify to exist? Commented Nov 24, 2009 at 0:24
  • sounds like he might be relying on native JSON support in the browser, if it works in Firefox and not in IE - developer.mozilla.org/en/Using_JSON_in_Firefox Commented Nov 24, 2009 at 0:28
  • @Russ: probably, yes. I'd assume OP would say both IE6 and 7 don't work then. Commented Nov 24, 2009 at 0:49
  • yep - Ive not tried IE7 but I am using the browsers built in JSON object. Commented Nov 24, 2009 at 8:50

5 Answers 5

36

The json2 library at https://github.com/douglascrockford/JSON-js is exactly what you're looking for. You can include it unconditionally, and it adds JSON.parse and JSON.stringify to your global namespace (only if there isn't one defined yet). It won't mess with any built-in JSON. From its source:

if (!this.JSON) {
    this.JSON = {};
}
...
if (typeof JSON.stringify !== 'function') {
...
if (typeof JSON.parse !== 'function') {

That's comprehensive! Even if for some reason you already have JSON.stringify but not JSON.parse (or vice versa) it'll still do the right thing, leaving the original ones in place.

Sign up to request clarification or add additional context in comments.

1 Comment

link is throwing error atm. So here's a backup github.com/douglascrockford/JSON-js
4

Your version of firefox might be having built-in support for the JSON library. But ideally you should include the JSON js library from json.org (make a copy of it in your hosted domain).

1 Comment

Yes and I think recent versions of the json library detect if there's a native json to use.
2

I also met this issue, you can load json2.js before using JSON. refer to this link.

Comments

1

Use the JSON-js made avaliable on Github by Douglas Crockford it makes the JSOn object avaliable in browsers which dont support the JSOn object natively just include a single js file in ur page which uses JSOn object. https://github.com/douglascrockford/JSON-js

Also Check out this link http://json.org/js.html

Comments

0

Simply check for JSON.stringify and if it doesn't exist, use some other method to parse data into a JSON string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.