Skip to content

a_22. Using JSONLoader

Fahim Chowdhury edited this page Sep 24, 2013 · 1 revision

#About This class lets you load JSON objects via JSONP (cross-domain).

#Usage

First you need to create a URLRequest and provide it with your url.

var urlRequest = new URLRequest("http://gdata.youtube.com/feeds/api/videos");

##(Optional) Add URL Variables

To add url variables you to your URLRequest you create a URLVariables class and use the 'set' method to set the variables. Then attach the URLVariables object to the URLRequest.

var variables = new URLVariables();
variables.set("q","foo");
variables.set("alt","json");
urlRequest.data = variables;

Now create a JSONLoader class and add an event listener which listens for the 'Event.COMPLETE' on the JSONLoader then pass in the URLRequest into the load method.

var jsonLoader = new JSONLoader();
Event.addListener(jsonLoader,Event.COMPLETE,loadedJSON);
jsonLoader.load(urlRequest);

function loadedJSON(event)
{
	console.log(event.args);
				
}

On your complete method you will receive an object in which it will contain a variable called 'args'. This will contain the JSON data.

Clone this wiki locally