Using JSON

16 Dec

Been using the actionscript JSON class from JSON.org and found a bug in the implementation. If you’re getting exponential numbers from your JSON service, an error is thrown. Something like this will cause you grief:

{
    "bigNumber":1.513e8
}

Found a way to bypass this, but using such numbers severely affects the stability of Flash 8. I’ve had Flash 8 disappear instantly after compiling code with such numbers. For some reason, this gets better after you save your file? No idea why, but it’s a thing to look out for.

My rewite is really simple. On line 268 (number-function) I’ve added this:

while (this.next() && (ch>='0' && ch<='9') || ch=='E'  || ch=='e' || ch=='-' || ch=='+') {
16 December 2005 at 6:26 pm

14 Responses to Using JSON

  1. avatar photo
    Christian 10 January 2006 at 8:05 pm #

    Can you show me how you get the JSON string into flash?

    load vars, load xml or via xml webservice

    I could find a contact e-mail on your website, is this true?

  2. avatar photo
    Jensa 10 January 2006 at 9:03 pm #

    import JSON;
    jsondata.onLoad = function(success){
    if(success){
      _global.jd = JSON.parse( unescape(this.toString()) );
    } else {
      trace(“unable to load JSON data”);
    }
    }


    If I only had slightly more spare time I’d write a tutorial for Flashmagazine on this…

    J

  3. avatar photo
    Jens Lehmann 27 February 2006 at 9:25 pm #

    There is just one problem in your implementation. You will never see a + in strings or numbers, since you use unescape().

    For Example

    {
    }

    will both give false results. “plus” will contain a space after parsing, bignumber will fail because “bigNumber” is unescaped to “1.513e 8”.

  4. avatar photo
    Jensa 27 February 2006 at 10:00 pm #

    Hi Jens,
    That’s true. This was the only way around the bug that I could find. By doing this, the class now supports the “e” and “E”, but it still does not support the “+” as you say. Feel free to post a solution here if you find one.

    J

  5. avatar photo
    Jens Lehmann 09 March 2006 at 12:26 am #

    try {
      _global.jd = JSON.parse(str);
    } catch (ex) {
    }
    };

  6. avatar photo
    Jensa 09 March 2006 at 2:31 am #

    Good point Jens! (Better AS2-style catching of errors)

  7. avatar photo
    fernando trasvina 05 January 2007 at 2:33 am #

    i just found a very stupid way to succesfully parse json strings.

  8. avatar photo
    Michael Rabinovich 06 April 2007 at 5:07 am #

    lemme show you how i’ve done this.
    This combines the onLoad handler that will let u know if you can’t load the json AND a parse function that will nicely throw lower level exceptions if the json is malformed.
    Dont forget to use
    import JSON
    in your actionscript, after saving the json class as a separate .as file (and don’t forget to CAPITALIZE the file name as in JSON.as).


    import JSON

    jsondata.onLoad = function(success){

    if(success){

      parsing( unescape(this.toString()) );
     
    } else {

      trace(“unable to load JSON data”);

    }

    }


    function parsing(jsonStr) {

    var json = new JSON();
      try {
     
      trace(o.fuck);
      } catch(ex) {
      }
    }

  9. avatar photo
    0 19 July 2007 at 4:26 am #

    import JSON;


    jsondata.onLoad = function(success){

    if(success){

      _global.jd = JSON.parse( unescape(this.toString()) );

    } else {

      trace(“unable to load JSON data”);

    }

    }

        _global.jd = JSON.parse( unescape(this.toString()) );

    I get this with every tutorial i try using JSON. Can you advise?

  10. avatar photo
    Jensa 27 July 2007 at 4:35 pm #

    Hi Anonymous,
    From the looks of that error message, it seems that you have done some changes to your class that causes this error, so it’s not JSON that is the problem. Static classes and variables have to follow certain rules and it looks like you are breaking one of them? I can’t tell which from the code you sent.

    J

  11. avatar photo
    narunas 01 August 2007 at 3:29 am #

    oh com’ on!!!
    **
    JSON.parse( unescape(this.toString()) );
    **
    call is assuming that the class JSON has a static function called “parse”

    import JSON;
    jo.parse(unescape(String));

    but then consider the scope where you are calling it…

  12. avatar photo
    Jensa 01 August 2007 at 6:52 am #

    Ooops. Narunas is right. I should have spotted that. Must not answer comments just before I go to bed…

    J

  13. avatar photo
    JB 16 June 2008 at 12:53 pm #

     

    need to change the “obj” to “o”

    Cheers for the code!!

  14. avatar photo
    Bren 25 March 2010 at 5:31 pm #

    Hey Jensa, thanks for the post - it fixed a minor quibbling bug that I’ve wasted a good hour on and much searching on. Not bad for 2005 code! Thanks.

  15.