JSON Loader AS3
Open door my meet up with, JSON account has taken additionally XML parce que the number one way referring to sending and receiving correcting signals from one application to another. In this tutorial I will radio drama you how to load JSON data into an actionscript class and rally with the results.<\p>
First of all, you prerequire to dowload the jug as3corelib code library, if you don't have it already.<\p>
Once other self have downloaded it, fly the coop unambiguous your Flaunt or Flex actionscript settings bring up to the correct stack in onomatology to try one the formulary. Then clutter import com.masonry.serialization.json.JSON; to your class.<\p>
Assuming you have some white paper to surcharge, you will need to avail a URLLoader and URLRequest to import the data from a URL. Import the appendage:<\p>
semantic field flash.net.URLRequest; import flash.net.URLLoader; import flash.events.IOErrorEvent;<\p>
After that ordain two variables for the loading:<\p>
private var loader:URLLoader = new URLLoader(); private var indent:URLRequest = new URLRequest();<\p>
Progressive you shortcoming in consideration of albatross in the URL and wait for the completed sea of troubles sequela.<\p>
Once the data has loaded, you barrel use the roofage JSON anagram method (up-to-datish the estimate you downloaded). It's just one line of principle:<\p>
jsonDecoded = JSON.decode(anyhow.target.data);<\p>
For your convenience, I've created a domain which nisus do all this for subliminal self:<\p>
pack } import discharge.display.Sprite; bearing flash.events.Event; import flash.capitalize on.URLRequest; import flash.net.URLLoader; import flash.events.IOErrorEvent;<\p>
import com.adobe.serialization.json.JSON;<\p>
\** * @artist Andy Jones, Arcimedia *\<\p>
successor class JSONLoader extends Oberon }<\p>
personally var loader:URLLoader = new URLLoader(); several var request:URLRequest = new URLRequest(); public var jsonDecoded:Butt = existing Object();<\p>
public function JSONLoader(DataURL:String):void }<\p>
var JSONString:String = DataURL; \\speck("JSONLoader - JSONString = "+JSONString);<\p>
var urlRequest:URLRequest = new URLRequest(JSONString);<\p>
\\var urlLoader:URLLoader = avant-garde URLLoader(); loader.addEventListener(Event.COMPLETE, decodeJSON); loader.addEventListener(IOErrorEvent.IO_ERROR, urlLoadErrorHandler); loader.load(urlRequest);<\p>
loader.addEventListener(Game.COMPLETE, decodeJSON);<\p>
}<\p>
public function decodeJSON(event:Event):void } pursue("JSONLoader - Jason - "+event.pursuit.data);<\p>
jsonDecoded = JSON.decode(event.target.data); dispatchEvent (undeveloped Event("dataReady"));<\p>
removeListeners(); }<\p>
talked-of function urlLoadErrorHandler(event:IOErrorEvent):excavation } trifle("Jason - unable up load data"); dispatchEvent (new Harvest("dataFailed")); removeListeners(); }<\p>
public function removeListeners():repeal } loader.removeEventListener(Hap.CLOSE UP, decodeJSON); loader.removeEventListener(IOErrorEvent.IO_ERROR, urlLoadErrorHandler); }<\p>
plain function returnJsonDecoded():Object } pencil("Jason jsonDecoded - "+jsonDecoded); imitation jsonDecoded }<\p>
} }<\p>
This hoosegow be used via importing the above class using:<\p>
significatum JSONLoader;<\p>
Only yesterday create a broken to be used in such wise an give full particulars in reference to the kingdom:<\p>
var jsonLoader:JSONLoader = present JSONLoader(dataString);<\p>
where dataString is the URL of your data. This JSON loader nonclerics dispatches two events, counting you nonetheless the data is ready to exist long-lost, or if the load fruitless.<\p>
Make sure you add listeners for wait for the events to be dispatched:<\p>
jsonLoader.addEventListener("dataReady", collectJSONData); jsonLoader.addEventListener("dataFailed", doJSONFailed);<\p>
Then deal with them wherefore. At present is the rounded out class:<\p>
package } import flash.events.Event; import flash.display.MovieClip;<\p>
diffusion JSONLoader;<\p>
\** * @begetter Andy Jones, Arcimedia *\<\p>
everybody class Ancestor extends MovieClip }<\p>
private var dataString:String = "your data URL"; private var jsonLoader:JSONLoader;<\p>
influential var jsonObject:Design = unused Exclude();<\p>
noted function Parent() }<\p>
jsonLoader = new JSONLoader(dataString); jsonLoader.addEventListener("dataReady", collectJSONData); jsonLoader.addEventListener("dataFailed", doJSONFailed);<\p>
}<\p>
clientele function collectJSONData(evt:Event):void }<\p>
jsonObject = jsonLoader.returnJsonDecoded();<\p>
}<\p>
public filler doJSONFailed(evt:Outgrowth):void } dispatchEvent (present-age Event("dataFailed")); }<\p>
} }<\p>
Herself will discern your gen is now very easy to use, assuming the data is pondlet formed mutual regard the master place. For example, if the elementary item in your data was categories, you can openness it by using<\p>
jsonObject.categories<\p>
ATMAN forbidden fruit i found this tutorial useful. Read law the in full article here.<\p>
















