6

All, I'm trying to parse some JSON and IE is giving me an error but not surprisingly the other browsers are ok with it. Here is my code:

var result = JSON.parse(data.result); 
var uploadType = result[0].upload_type;
var filename = result[0].name;
var insert_id = result[0].insert_id;

I'm getting an error on the first line. Any idea how to make this IE proof?

Thanks!

Spudley
  • 166,037
  • 39
  • 233
  • 307
user1048676
  • 9,756
  • 26
  • 83
  • 120
  • 1
    Possible duplicate ['JSON' is undefined error in IE only](http://stackoverflow.com/questions/5093582/json-is-undefined-error-in-ie-only) – Andreas Jul 11 '12 at 21:26

5 Answers5

14

Internet Explorer does not support JSON.parse before version 8. You may use jQuery.parseJSON instead (as I see you have tagged the question ).

1

Internet Explorer doesnt support JSON.parse, reference the json2 script at and you will get the same functionality.

Jason Quinn
  • 2,443
  • 3
  • 28
  • 36
0

You haven't specified the IE version, but if you're using IE8 or earlier, it doesn't support the JSON object natively.

You'll need a third-party library to polyfill this feature.

jQuery is one option.

You can find a bunch of other options here: https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • You're correct, it's IE8. I'm also using jQuery. How can I make this snippet work with jQuery? – user1048676 Jul 11 '12 at 21:33
  • the context of your code snippet is unclear, but jquery provides transparent json encoding/decoding for its ajax calling routines. If you're receiving the `result` variable via an ajax call, jquery should be able to provide it already decoded. If the context is not ajax, then you can use [`$.parseJSON()`](http://api.jquery.com/jQuery.parseJSON/). Hope that helps. – Spudley Jul 12 '12 at 06:20
0

I've tried this one and this worked for me: https://github.com/flowersinthesand/jquery-stringifyJSON

The previous solutions doesn't work for me, even this: https://github.com/douglascrockford/JSON-js

danieltc07
  • 183
  • 4
  • 17
0

For those who cannot use third libraries, you can always make use of

eval('var data = ' + request.responseText);

to get the same functionality

lambidu
  • 1,038
  • 10
  • 19
  • 1
    Be careful about using eval function, because that can inject javascript. Use only if you have full control about the contents. Imagine the "request.responseText" contains some part of a field inserted by some user. The user can fill javascript code to be injected on the page! – Manuel Romeiro Dec 11 '19 at 14:08
  • 1
    You're right, `eval()` isn't safe at all, but we speak here about IE... :D – lambidu Dec 12 '19 at 16:27