www.webdeveloper.com
Results 1 to 4 of 4

Thread: How to use json2?

  1. #1
    Join Date
    Jul 2010
    Posts
    11

    How to use json2?

    I have the following code. I want to convert obj to a JSON string and output it to the document. But my chrome browser doesn't show anything. If I run it in the Chrome Development Tools. The JSON string shows up. Does anybody seem what is wrong with my script? Thanks!

    <html>
    <body>
    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"><script/>
    <script>
    var obj={x:1, y:2};
    var json_data = JSON.stringify(obj);
    document.write('xx' + json_data + '<br/>');
    </script>
    </body>
    </html>

  2. #2
    Join Date
    May 2010
    Posts
    213
    Call me stupid, but isn't {x:1, y:2} already a json value? i think you want to pass an actual object and then the stingify converts it to a... well... to a string...

    You know what i mean? Like if you did var b = new Object().
    i love easter eggs
    (if you got any creative easter eggs, send me a PM)

  3. #3
    Join Date
    Sep 2007
    Posts
    315
    I don't know JSON.
    It will be
    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
    instead of
    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"><script/>

    Code:
     
    <html>
    <body>
    <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
    <script type="text/javascript">
    var obj={x:1, y:2};
    var json_data = JSON.stringify(obj);
    document.write('xx' + json_data + '<br/>');  // xx{"x":1,"y":2}
    </script>
    </body>
    </html>
    Code:
      
    <html>
    <body>
    <script type="text/javascript">
    var obj={x:1, y:2};
    var json_data = JSON.stringify(obj);
    document.write('xx' + json_data + '<br/>');  // xx{"x":1,"y":2}
    </script>
    </body>
    </html>
    The output of the codes above will be:
    xx{"x":1,"y":2}
    in Firefox 4.0b9
    Last edited by Ayşe; 01-09-2012 at 02:51 PM.
    Bismillahirrahmanirrahîm
    Hamd, Âlemlerin Rabbi, Rahmân, Rahîm, hesap ve ceza gününün (ahiret gününün) maliki Allah'a mahsustur. (Allahım!) Yalnız sana ibadet ederiz ve yalnız senden yardım dileriz. Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.

  4. #4
    Join Date
    Oct 2012
    Posts
    1
    In Google Chrome (v. 22.0.1) the output is xx{"x":1,"y":2} too ... the method stringfy is just to convert the object array in a string array... in some browsers you will need this convertion... but in modern browsers you dont need to do this. Read more about JSON methods: http://www.json.org/js.html

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles