Get and parse JSON in Actionscript

前端 未结 3 1767
攒了一身酷
攒了一身酷 2020-12-19 09:15

What I want to do is make a get request to this URL: http://api.beatport.com/catalog/3/most-popular, which should return some JSON and then parse out certain information fro

相关标签:
3条回答
  • 2020-12-19 09:24

    Alternatively if you're using Flash Player 11 or AIR 3.0 or higher you can use the built in JSON object to decode your JSON. It's a top level object so you dont 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

    0 讨论(0)
  • 2020-12-19 09:30

    I believe the as3corelib has a JSON serializer and deserializer

    You can use those instead of re-inventing the wheel and writing parsing logic afresh.

    0 讨论(0)
  • 2020-12-19 09:31
    1. Add the corelib.swc to your library path.

    2. Import the JSON library: import com.adobe.serialization.json.JSON;

    3. 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);
      }
      
    4. Parse the results with JSON.decode(results).

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

    0 讨论(0)
提交回复
热议问题