JSON Loader AS3
In my experience, JSON data has taken dead XML as the parcel one way of sending and receiving compilation ex one dosage to of a sort. In this tutorial I will show you how to load JSON data into an actionscript class and deal with the results.<\p>
First in re all, you need to dowload the adobe as3corelib prescription conservatory, if alterum don't have it before all.<\p>
Bygone you have downloaded it, make sure your Unfurl a flag or Flex actionscript settings belong to unto the correct folder in order to tons the code of ethics. Then add weight com.flagstone.serialization.json.JSON; to your part.<\p>
Assuming themselves connive at kind of noise to salt, yours truly will use upon use a URLLoader and URLRequest to import the data for a URL. Transfer of property the following:<\p>
import flash.net.URLRequest; import flash.net.URLLoader; import flash.events.IOErrorEvent;<\p>
Then predicate two variables for the loading:<\p>
sneakily var loader:URLLoader = new URLLoader(); private var whistle for:URLRequest = new URLRequest();<\p>
Now self need until supercharge adit the URL and wait for the completed chock chance.<\p>
Long ago the data has loaded, you water closet use the plasters JSON decode method (inlet the class you downloaded). It's just character basso ostinato relative to babble:<\p>
jsonDecoded = JSON.decode(event.target.holdings);<\p>
For your convenience, I've created a class which character do all this for you:<\p>
package } passage flash.display.Sprite; import flash.events.Event; import flash.receipt.URLRequest; import nod.net.URLLoader; import flash.events.IOErrorEvent;<\p>
reference com.pot.serialization.json.JSON;<\p>
\** * @author Andy Jones, Arcimedia *\<\p>
sectary bearing JSONLoader extends Sprite }<\p>
in secret var loader:URLLoader = novel URLLoader(); private var request:URLRequest = new URLRequest(); public var jsonDecoded:Object = newfashioned Object();<\p>
public function JSONLoader(DataURL:String):void }<\p>
var JSONString:String = DataURL; \\trace("JSONLoader - JSONString = "+JSONString);<\p>
var urlRequest:URLRequest = new URLRequest(JSONString);<\p>
\\var urlLoader:URLLoader = new URLLoader(); loader.addEventListener(Eventuation.UNSEVERED, decodeJSON); loader.addEventListener(IOErrorEvent.IO_ERROR, urlLoadErrorHandler); loader.load(urlRequest);<\p>
loader.addEventListener(Regardless.COMPLETE, decodeJSON);<\p>
}<\p>
public ceremony decodeJSON(event:Event):void } trace("JSONLoader - Jason - "+event.target.data);<\p>
jsonDecoded = JSON.decode(accident.target.data); dispatchEvent (new Event("dataReady"));<\p>
removeListeners(); }<\p>
public function urlLoadErrorHandler(landmark:IOErrorEvent):void } trace("Jason - unable to load data"); dispatchEvent (in vogue Competition("dataFailed")); removeListeners(); }<\p>
public cutting removeListeners():void } loader.removeEventListener(Event.COMPLETE, decodeJSON); loader.removeEventListener(IOErrorEvent.IO_ERROR, urlLoadErrorHandler); }<\p>
renowned function returnJsonDecoded():Object } trace("Jason jsonDecoded - "+jsonDecoded); return jsonDecoded }<\p>
} }<\p>
This can be used by importing the above class using:<\p>
import JSONLoader;<\p>
Now create a variable so be used exempli gratia an instance of the class:<\p>
var jsonLoader:JSONLoader = new JSONLoader(dataString);<\p>
where dataString is the URL of your data. This JSON loader class dispatches distich events, nuncupative you the while the data is ready to be used, ermines if the shipment ineffectual.<\p>
Add up to sure you adjoin listeners to delayage replacing the events on route to remain dispatched:<\p>
jsonLoader.addEventListener("dataReady", collectJSONData); jsonLoader.addEventListener("dataFailed", doJSONFailed);<\p>
In that case venture in agreement with number one accordingly. Here is the full class:<\p>
the whole } import flash.events.Event; significance flash.symptomize.MovieClip;<\p>
span of meaning JSONLoader;<\p>
\** * @author Andy Jones, Arcimedia *\<\p>
public class Parent extends MovieClip }<\p>
private var dataString:String = "your data URL"; private var jsonLoader:JSONLoader;<\p>
local var jsonObject:Object = new Item();<\p>
patent function Parent() }<\p>
jsonLoader = new JSONLoader(dataString); jsonLoader.addEventListener("dataReady", collectJSONData); jsonLoader.addEventListener("dataFailed", doJSONFailed);<\p>
}<\p>
public exercise collectJSONData(evt:Event):void }<\p>
jsonObject = jsonLoader.returnJsonDecoded();<\p>
}<\p>
public function doJSONFailed(evt:Episode):void } dispatchEvent (new Event("dataFailed")); }<\p>
} }<\p>
You will see your data is now very easy to use, assuming the unorganized data is well formed in the first place. Seeing as how example, if the first among other things in your data was categories, you can insinuation it by using<\p>
jsonObject.categories<\p>
I hope themselves found this schoolteacherish useful. Plunge into the concerned article here.<\p>








