Nulls.net: Invalid use of Null Error : use, null, invalid, error :)

JSON Loader AS3  


JSON Loader AS3  

Article by Andy Jones







In my experience, JSON data has taken over XML as the number one way of sending and recieving data from one application to another. In this tutorial I will show you how to load JSON data into an actionscript class and deal with the results.

First of all, you need to dowload the adobe as3corelib code library, if you don’t have it already.

Once you have downloaded it, make sure your Flash or Flex actionscript settings refer to the correct folder in order to load the code. Then add import com.adobe.serialization.json.JSON; to your class.

Assuming you have some data to load, you will need to use a URLLoader and URLRequest to import the data from a URL. Import the following:

import flash.net.URLRequest;import flash.net.URLLoader;import flash.events.IOErrorEvent;

Then declare two variables for the loading:

private var loader:URLLoader = new URLLoader() ;private var request:URLRequest = new URLRequest();

Now you need to load in the URL and wait for the completed load event.

Once the data has loaded, you can use the adobe JSON decode method (in the class you downloaded). It’s just one line of code:

jsonDecoded = JSON.decode(event.target.data);

For your convenience, I’ve created a class which will do all this for you:

package{import flash.display.Sprite;import flash.events.Event;import flash.net.URLRequest;import flash.net.URLLoader;import flash.events.IOErrorEvent;

import com.adobe.serialization.json.JSON;

/*** @author Andy Jones, Arcimedia*/

public class JSONLoader extends Sprite{

private var loader:URLLoader = new URLLoader() ;private var request:URLRequest = new URLRequest();public var jsonDecoded:Object = new Object();

public function JSONLoader(DataURL:String):void{

var JSONString:String = DataURL;//trace(“JSONLoader – JSONString = “+JSONString);

var urlRequest:URLRequest = new URLRequest(JSONString);

//var urlLoader:URLLoader = new URLLoader();loader.addEventListener(Event.COMPLETE, decodeJSON);loader.addEventListener(IOErrorEvent.IO_ERROR, urlLoadErrorHandler);loader.load(urlRequest);

loader.addEventListener(Event.COMPLETE, decodeJSON) ;

}

public function decodeJSON(event:Event):void{trace(“JSONLoader – Jason – “+event.target.data);

jsonDecoded = JSON.decode(event.target.data);dispatchEvent (new Event(“dataReady”));

removeListeners();}

public function urlLoadErrorHandler(event:IOErrorEvent):void{trace(“Jason – unable to load data”);dispatchEvent (new Event(“dataFailed”));removeListeners();}

public function removeListeners():void{loader.removeEventListener(Event.COMPLETE, decodeJSON) ;loader.removeEventListener(IOErrorEvent.IO_ERROR, urlLoadErrorHandler);}

public function returnJsonDecoded():Object{trace(“Jason jsonDecoded – “+jsonDecoded);return jsonDecoded}

}}

This can be used by importing the above class using:

import JSONLoader;

Now create a variable to be used as an instance of the class:

var jsonLoader:JSONLoader = new JSONLoader(dataString);

where dataString is the URL of your data. This JSON loader class dispatches two events, telling you when the data is ready to be used, or if the load failed.

Make sure you add listeners to wait for the events to be dispatched:

jsonLoader.addEventListener(“dataReady”, collectJSONData);jsonLoader.addEventListener(“dataFailed”, doJSONFailed);

Then deal with them accordingly. Here is the full class:

package{import flash.events.Event;import flash.display.MovieClip;

import JSONLoader;

/*** @author Andy Jones, Arcimedia*/

public class Parent extends MovieClip{

private var dataString:String = “your data URL”;private var jsonLoader:JSONLoader;

public var jsonObject:Object = new Object();

public function Parent(){

jsonLoader = new JSONLoader(dataString);jsonLoader.addEventListener(“dataReady”, collectJSONData);jsonLoader.addEventListener(“dataFailed”, doJSONFailed);

}

public function collectJSONData(evt:Event):void{

jsonObject = jsonLoader.returnJsonDecoded();

}

public function doJSONFailed(evt:Event):void{dispatchEvent (new Event(“dataFailed”));}

}}

You will see your data is now very easy to use, assuming the data is well formed in the first place. For example, if the first item in your data was categories, you can access it by using

jsonObject.categories

I hope you found this tutorial useful. Read the full article here.



About the Author

Andy has worked in web development for 12 years and is Head of Development for Arcimedia.


Most Related Posts

  • No Related Posts Found

You must be logged in to post a comment.


htcell