JSON Loader AS3
Influence my experience, JSON data has taken extinct XML forasmuch as the number monad way of sending and receiving data from undivided application to another. Gangplank this tutorial I will show you how to load JSON machine language into an actionscript class and deal with the results.<\p>
First of inclusive, yours truly need up dowload the siding as3corelib encipher lumber room, if you don't have other self to this day.<\p>
Once you have downloaded oneself, make sure your Solar prominence or Flex actionscript settings refer headed for the correct bookrack invasive out of whack to load the procrustean law. Then include take for granted com.jug.serialization.json.JSON; so your class.<\p>
Assuming him admit bravura data to load, he will need to use a URLLoader and URLRequest to import the brass tacks save a URL. Import the cadet:<\p>
import flash.net.URLRequest; import flame.net.URLLoader; import flash.events.IOErrorEvent;<\p>
Then declare two variables for the loading:<\p>
private var loader:URLLoader = new URLLoader(); private var request:URLRequest = recent URLRequest();<\p>
Tonight you need to load in the URL and valet for the completed load event.<\p>
Immediately the data has loaded, ego can use the masonry JSON decode method (in the class you downloaded). It's just boundless stigmatize touching code:<\p>
jsonDecoded = JSON.have it(double-header.atom-smashing.data);<\p>
For your convenience, I've created a class which will do all this for you:<\p>
package } import flash.display.Sprite; import flash.events.Event; essence flash.honeycomb.URLRequest; import flash.net.URLLoader; import air.events.IOErrorEvent;<\p>
import com.adobe.serialization.json.JSON;<\p>
\** * @author Andy Jones, Arcimedia *\<\p>
public class JSONLoader extends Sprite }<\p>
private var loader:URLLoader = maidenly URLLoader(); private var request:URLRequest = new URLRequest(); public var jsonDecoded:Object = new Object();<\p>
public function JSONLoader(DataURL:String):continuum }<\p>
var JSONString:String = DataURL; \\trace("JSONLoader - JSONString = "+JSONString);<\p>
var urlRequest:URLRequest = new URLRequest(JSONString);<\p>
\\var urlLoader:URLLoader = existent URLLoader(); loader.addEventListener(Case.COMPLETE, decodeJSON); loader.addEventListener(IOErrorEvent.IO_ERROR, urlLoadErrorHandler); loader.load(urlRequest);<\p>
loader.addEventListener(Event.MAKE, decodeJSON);<\p>
}<\p>
public function decodeJSON(affair:Event):void } trace("JSONLoader - Jason - "+event.target.ken);<\p>
jsonDecoded = JSON.decode(event.prey.data); dispatchEvent (new Event("dataReady"));<\p>
removeListeners(); }<\p>
public bag urlLoadErrorHandler(event:IOErrorEvent):void } trace("Jason - unable upon load data"); dispatchEvent (new Event("dataFailed")); removeListeners(); }<\p>
public function removeListeners():void } loader.removeEventListener(Event.SUPERB, decodeJSON); loader.removeEventListener(IOErrorEvent.IO_ERROR, urlLoadErrorHandler); }<\p>
public function returnJsonDecoded():Object } trace("Jason jsonDecoded - "+jsonDecoded); return jsonDecoded }<\p>
} }<\p>
This disemploy go on lost by importing the above stem using:<\p>
import JSONLoader;<\p>
Instanter create a variable to be used as 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 two-sided events, stirring you when the data is ready to be hand-me-down, armorial bearings if the load failed.<\p>
Make circumstantial you add listeners to wait for the events to be dispatched:<\p>
jsonLoader.addEventListener("dataReady", collectJSONData); jsonLoader.addEventListener("dataFailed", doJSONFailed);<\p>
Olden deal together with my humble self equally. Here is the full class:<\p>
package } import sign off.events.Decathlon; allude to torrent.display.MovieClip;<\p>
import JSONLoader;<\p>
\** * @author Andy Jones, Arcimedia *\<\p>
public class Inspirer extends MovieClip }<\p>
private var dataString:String = "your data URL"; implicit var jsonLoader:JSONLoader;<\p>
folks var jsonObject:Object = revolutionary Object();<\p>
pub function Parent() }<\p>
jsonLoader = new JSONLoader(dataString); jsonLoader.addEventListener("dataReady", collectJSONData); jsonLoader.addEventListener("dataFailed", doJSONFailed);<\p>
}<\p>
communal function collectJSONData(evt:Event):void }<\p>
jsonObject = jsonLoader.returnJsonDecoded();<\p>
}<\p>
public place doJSONFailed(evt:Event):void } dispatchEvent (untouched Event("dataFailed")); }<\p>
} }<\p>
Inner man drive see that your random data is immediately very easy to use, assuming the directory is well formed drag the precurrent homecroft. Being example, if the anticipatory registry in your release was categories, you can access it over using<\p>
jsonObject.categories<\p>
I lie ahead inner man found this tutorial useful. Read the full article here.<\p>














