获取并解析JSON中的Actionscript [英] Get and parse JSON in Actionscript

查看:329
本文介绍了获取并解析JSON中的Actionscript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是使一个GET请求来此URL:<一个href="http://api.beatport.com/catalog/3/most-popular">http://api.beatport.com/catalog/3/most-popular,它应该返回一些JSON,然后从中解析出某些信息。

我怎么会去在ActionScript 3中这样做呢?我更关心的是搞清楚如何让数据送入一个J​​SON解析器,而不是解析JSON,因为似乎有很多关于解析JSON的问题。我想这样做在AS3的原因是,我有一个3D动画的可视化设置和我想要得到这个数据,解析出相关的位,然后在可视化显示解析位。

我愿意这样做的任何其他方式,可以很容易地与闪存集成,除了AS3,如果有一个更简单的方法来做到这一点的另一种语言。

解决方案
  1. 添加 corelib.swc 到库的路径。

  2. 导入JSON库:进口com.adobe.serialization.json.JSON;

  3. 与code这样的事情打电话给你的服务:

      VAR要求:的URLRequest =新的URLRequest();
    request.url = YOUR_ENDPOINT
    request.requestHeaders = [新URLRequestHeader(内容类型,应用/ JSON);
    request.method = URLRequestMethod.GET;
    VAR装载机:的URLLoader =新的URLLoader();
    loader.addEventListener(引发Event.COMPLETE,接收);
    loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,notAllowed);
    loader.addEventListener(IOErrorEvent.IO_ERROR,NOTFOUND);
    loader.load(要求);
    
    保护功能接收(事件:事件):无效
    {
         VAR myResults:阵列= JSON.de code(event.target.data);
    }
     

  4. 解析结果与 JSON.de code(结果)

的as3corelib在这里维护:<一href="https://github.com/mikechambers/as3corelib#readme">https://github.com/mikechambers/as3corelib#readme.

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.

解决方案

  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.

这篇关于获取并解析JSON中的Actionscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆