Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

flash - Get and parse JSON in Actionscript

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 from it.

How would I go about doing this in Actionscript 3? I'm more concerned with figuring out how to get the data to feed to a JSON parser rather than parsing the JSON, since there seem to be plenty of questions about parsing JSON. The reason I want to do this in AS3 is that I have a 3D flash visualization set up and I want to get this data, parse out the relevant bits, and then display the parsed bits in the visualization.

I'm open to any other ways of doing this that can easily be integrated with Flash besides AS3 if there's an easier way to do it in another language.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...