SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question about JSON

    Hello,

    I have read this very interesting article: http://www.json.org/js.html

    My question is: is it safe to use native JSON parser nowadays, or should I worry that some browsers may not support it?

    If I have to include the json script, how should I do it? My script is wrapped in an anonymous function. Should the json script be included there? Outside the scope of this anonymous function?


  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rhgiant View Post
    My question is: is it safe to use native JSON parser nowadays, or should I worry that some browsers may not support it?
    It would still be a good idea to include it, as IE6/7 don't have native JSON parsing (http://caniuse.com/#feat=json)

    Quote Originally Posted by rhgiant View Post
    If I have to include the json script, how should I do it? My script is wrapped in an anonymous function. Should the json script be included there? Outside the scope of this anonymous function?
    If you use the json2.js it will only add the JSON methods if they do not already exist, for example:

    Code:
    // If the JSON object does not yet have a stringify method, give it one.
    
        if (typeof JSON.stringify !== 'function') {
            JSON.stringify = function (value, replacer, space) {
    If you use an anonymous function closure around your script, you can pass in the JSON object as an parameter to the closure, the only pre-requisite is of course that the JSON script is executed before you use it :-)

    Code javascript:
    ( function ( JSON ) {
     
     
      //safe to use JSON 
     
     
    } )( JSON );

    And for example, if you use a library like jQuery, you could pass it into your closure as well.

    Code javascript:
    ( function ( JSON, $ ) {
     
     
      //safe to use JSON and $ as a jQuery alias
     
     
    } )( JSON, jQuery );

    While it is certainly possible to include the JSON library in your closure, it would probably not be feasible to do this with a library like jQuery ;-)
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2011
    Posts
    199
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great answer. Thank you so much. Yes, JQuery would be a bit too big.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •