Get and parse JSON in ActionScript

I want to request this URL: http://api.beatport.com/catalog/3/most-popular , which should return some JSON, and then parse certain information from it.

How do I do this in ActionScript 3? I'm more worried about how to get the data to feed to the JSON parser instead of parsing JSON, as there seem to be a lot of questions about parsing JSON. The reason I want to do this in AS3 is because I have 3D flash visualization installed and I want to get this data, analyze the corresponding bits and then display the analyzed bits in the visualization.

I am open to any other ways to do this, which integrates easily with Flash other than AS3, if there is an easier way to do this in another language.

+6
source share
3 answers
  • Add corelib.swc to your library path.

  • Import JSON library: import com.adobe.serialization.json.JSON;

  • Call your service with code something like this:

     var request:URLRequest=new URLRequest(); request.url=YOUR_ENDPOINT request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")]; request.method=URLRequestMethod.GET; var loader:URLLoader=new URLLoader(); loader.addEventListener(Event.COMPLETE, receive); loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, notAllowed); loader.addEventListener(IOErrorEvent.IO_ERROR, notFound); loader.load(request); protected function receive(event:Event):void { var myResults:Array=JSON.decode(event.target.data); } 
  • Parse the results using JSON.decode(results) .

as3corelib is supported here: https://github.com/mikechambers/as3corelib#readme .

+12
source

Alternatively, if you are using Flash Player 11 or AIR 3.0 or higher, you can use the built-in JSON object to decode your JSON. This is a top-level object, so you donโ€™t even need to import anything, just do:

 var decoded : Object = JSON.parse(loadedText); 

See: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html

+10
source

I believe that as3corelib has JSON serializer and deserializer

You can use them instead of reinventing the wheel and re-parsing logic.

+3
source

Source: https://habr.com/ru/post/912963/


All Articles