0

How well supported is JSON across browsers? I just tried the following:

<?php

    header('Content-type: application/json');
    $arr = array('name' => 'Lisa');
    echo json_encode($arr);
?>

and AJAX:

<script type="text/javascript">

    $.ajax({
        type: 'get',
        url: 'php-url-here',
        success: function(response){
            alert(response.name);   
        }
    });
</script>

This returns the right information from the JSON object, being Lisa, so the real question is, is the JSON response parsed because the browser supports it based on the content type passed or because the ajax function in JQuery supports it? What is the bulletproof way of doing this so it will be supported in all browsers old and new, or most.

David
  • 15,894
  • 22
  • 55
  • 66
stacker
  • 43
  • 5
  • http://stackoverflow.com/questions/891299/browser-native-json-support-window-json – koningdavid Feb 13 '14 at 20:31
  • JSON is nothing more than a string of characters, the browser receives nothing more than a string of characters, `$.ajax()` performs a check to see if the string is JSON-like, if yes then `$.ajax()` converts it into a usable JS notation such as an array or object. `$.ajax()` calls upon native JS functions and depending on the JS version which the browser has implemented then you either are or are not able to convert the string into a JS object or array – MonkeyZeus Feb 13 '14 at 20:39
  • http://caniuse.com/json – Blazemonger Feb 13 '14 at 20:45

5 Answers5

3

The response is parsed because jQuery recognizes it is JSON based on the Content-Type reported by the server. Native browser support for parsing JSON is universal at this point, but jQuery 1.x does include its own implementation as a fallback.

Therefore:

  1. If you write your own AJAX library and want to have automatic parsing you will have to code the triggering logic yourself. Unless you care about ancient browsers the parsing is built-in (JSON.parse).
  2. If you use jQuery you don't need to worry about anything as long as the server sends the correct internet media type.
  3. Even if the server is buggy you can instruct jQuery to parse the response as JSON (the dataType option).
Jon
  • 428,835
  • 81
  • 738
  • 806
0

At this current point in time, JSON is supported by most browsers:

http://caniuse.com/#search=JSON.parse

Stu1986C
  • 1,452
  • 1
  • 18
  • 34
0

Browsers aren't supposed to render or parse JSON by default. If you open a JSON page, it will just render as text.

If you wish to parse a JSON string, though, use JSON.parse(). It is supported by all major browsers.

What jQuery does here is that it recognizes that the response is JSON, and parses it.

Manishearth
  • 14,882
  • 8
  • 59
  • 76
0

JSON stands for JavaScript Object Notation - that's enough to tell you that everything that runs JavaScript can support it, one way or another.

However, it's nice to have a more limited JSON parser, because it's faster and safer. For example, calling var data = eval(JSONCode); works, but it's vulnerable to code injection. The browsers that have parsers specific to JSON are IE8+, FireFox 3.5+, Chrome 4+, Safari 4.0+, Opera 10.5+, and most mobile browsers (source). They do this through the native API, window.JSON.

If that's not enough compatibility for your application, there are a number of good JSON parsers out there, which, again, are preferable to eval because they're faster and safer. Here's one that polyfills the native API: http:// bestiejs.github.io/json3/. Another is included in jQuery.

twhb
  • 4,294
  • 2
  • 20
  • 23
0

Yes. JSON encoding and decoding is natively supported on[1]:

  • Mozilla Firefox 3.5+
  • Microsoft Internet Explorer 8+
  • Opera 10.5+
  • WebKit-based browsers (Apple Safari)
  • Blink-based browsers (e.g. Google Chrome, Opera)

*[1]http://en.wikipedia.org/wiki/JSON#Native_encoding_and_decoding_in_browsers

AnaCronIsm
  • 51
  • 1
  • 2