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;

}

}

Advertisements
Code Tip: parse variables response from backend into as3

4 thoughts on “Code Tip: parse variables response from backend into as3

  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’]

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 )

Google+ photo

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

Connecting to %s