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

About Bela Korcsog

Proud father of two children, happy husband to one wife. I've been programming various technologies and leading the development of huge projects for most of the last ten years. I've got some specific likes and dislikes through my experiences in the web site business but generally I'm pretty straightforward about it. Not a huge fan of the latest and greatest shiny toy (it took me four years to show an interest in Flash) I'm more than happy to code in any language that comes along (Actionscript is just so darn fun).
This entry was posted in Flash. Bookmark the permalink.

Comments are closed.