Home > ActionScript, Flash > Code Tip: parse variables response from backend into as3

Code Tip: parse variables response from backend into as3

Just a quick code bit I wanted to share, for those interested. Example below assumes that a response from backend is in this format:

id=someID&status=someStatus&message=someMessage

Here is how I parse it. If someone knows a better & faster way; please let me know.

function onResponseComplete(e:Event):void
{

var nameValuePairs:Array = unescape(e.target.data).split(‘&’);
var id:String;
var status:String;
var message:String;
var i:int = 0;
var length:int = nameValuePairs.length;
var nameValuePair:String;
var variable:String;
var value:String;

for (i = 0; i < length; ++i)
{

nameValuePair = nameValuePairs[i] as String;
variable = nameValuePair.substring(0, nameValuePair.indexOf(‘=’));
value = nameValuePair.substring(nameValuePair.indexOf(‘=’) + 1, nameValuePair.length);

if (variable == ‘id’) id = value;
if (variable == ‘status’) id = value;
if (variable == ‘message’) id = value;

}

}

Advertisement
Categories: ActionScript, Flash Tags: , ,
  1. September 22, 2011 at 10:31 am | #1

    You can save time n clean on code by this.

    import com.adobe.serialization.json.JSON;
    var jSonoBj = JSON.decode(event.currentTarget.data);
    //n u can access / assign data as
    jSonoBj['id'] / jSonoBj['status'] / jSonoBj['message']

  2. December 29, 2011 at 6:07 pm | #3
  3. February 8, 2012 at 3:44 pm | #4

    or also:

    var urlvars:URLVariables = new URLVariables(“id=someID&status=someStatus&message=someMessage”);
    trace(urlvars.id);

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.