Calling a JSON service function from Flash AS3

Here’s a quick way to call a JSON request without loading a bunch of Classes and wrangling Adobe’s methods:

I’ve used a simple serializer from Designvox which was ported from the Vegas framework at osflash.org. It suits my sensibilities and keeps my code down to a minimum.

var uvars:URLVariables = new URLVariables();
(add values using format uvars[name of var] or uvars.(name of var) = …)

var ureq:URLRequest = new URLRequest();
ureq.url = (JSON service gateway url)
ureq.method = URLRequestMethod.POST;
ureq.data = JSON.serialize(uvars);
ureq.contentType = “application/json”;

var uload:URLLoader = new URLLoader();
uload.addEventListener(Event.COMPLETE, this.complete);
uload.load(ureq);

private function complete(e:Event):void {
trace(e.currentTarget.data);

var do:Object = JSON.deserialize(e.currentTarget.data);
}

This is it. JSON serialized strings are stored in the ‘data’ of the URLRequest and when you complete the load you can use JSON.deserialize(e.data) to create a data object to interpret in your code.

Attached is the JSON class from designvox:

Download the JSON class from this link

This entry was posted in Flash. Bookmark the permalink.

Comments are closed.