A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: How to turn a string into a properly structured object

  1. #1
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269

    How to turn a string into a properly structured object

    Hi Chaps,

    I'm using an online service (heyzap.com if anyone wanted to know) for storing small amounts of data from my Flash app. The data is sent to the server as an object, which can contain any kind of structure including arrays, numbers, strings etc. However, when the object is returned back to the app, for some reason, it has been turned into a string representation of the object. I don't think it should be like that - but it is. So if I create a textbox on screen and write the code:

    myTextbox.text = receivedObject;

    it shows this:

    {"myArray":[{"myInteger":0,"myString":"string0","myData":[1,2,3]},{"myInteger":1,"myString":"string1","myData":[1,2,3]},{"myInteger":2,"myString":"string2","myData":[1,2,3]},{"myInteger":3,"myString":"string3","myData":[1,2,3]},{"myInteger":4,"myString":"string4","myData":[1,2,3]}]}

    That's just a test, and it tells me that all the structure is correct, but it's just a single string. Question is, without writing a complicated parser to deal with all the brackets and so forth, is there a simple way to turn that string back in an object again such that I can access the elements as I could the original object?

    Any help much appreciated.

    cheers,

    Ric.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Try tracing typeof(receivedObject). It could be that you're seeing the result of calling toString on that object. On the other hand, if it were anything other than a String, I'd have expected an error.

    If that is a String, try using the JSON part of the as3corelib to decode it.
    http://code.google.com/p/as3corelib/

  3. #3
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269
    Thanks 5Tons - it has to be said, you are a genius. Yes, it was a string, tracing typeOf confirmed that, and secondly, yes it had been JSON encoded. I don't even really know what that means, but I downloaded that as3corelib, and wrote the code

    var objectString:String = String(receivedObject);
    var decode:JSONDecoder = new JSONDecoder(objectString);
    var newObject:Object = decode.getValue();

    and lo and behold I now have my original object restored and in a state which I can use once again.

    The only thing which remains is for me to let heyzap know about this, because this is not how their api is supposed to work according to their documentation.

    Thanks a lot,

    Ric.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center