Flex获取服务器JSON数据并解析JSON

来源:互联网 发布:大数据知识点 编辑:程序博客网 时间:2024/05/22 06:48
Flex与服务器通信的常用方式 
标签
Java代码  收藏代码
  1. <mx:HTTPService id= "jsonService" url="http://localhost:8080/getData.json" showBusyCursor="true" method="POST" result="onCallResult(e)" />  

说明 url为服务器路径, showBusyCursor是否让鼠标显示忙碌状态, method为提交方式, result很重要是回调方法, 此方法中的e为ResultEvent对象, e.result可以取得服务器返回的数据 
发送请求,
Java代码  收藏代码
  1. jsonService.send();  

若有参数 
Java代码  收藏代码
  1. var params:URLVariables=new URLVariables();  
  2. params.param1 = param1;  // 直接  .参数 = 参数, 就行了  
  3. jsonService.send(param1 );  

脚本方式 
Java代码  收藏代码
  1. var jsonService:HTTPService=new HTTPService();  
  2. jsonService.url = "http://localhost:8080/getData.json"  
  3. jsonService.showBusyCursor=GlobalParamControl.showBusyCursor;  
  4. jsonService.addEventListener(ResultEvent.RESULT, ResultFunc); // 省略ResultFunc方法   
  5. // ...其它与上同理  


说明Flex扣件JSON需要as3corelib库的支持,你可以去http://code.google.com/p/as3corelib/进行下载,并且选中项目单击右键选择“项目属性(Project Properties) ”构建路径(Flex Build Path)“ 选中"库路径(Library Path)标签" 单击“添加SWC(Add SWC)”进行浏览添加库文件。 

以下为Flex的代码: 

Java代码  收藏代码
  1. import com.adobe.serialization.json.JSON;  
  2. public var goodsArr:Array;  
  3. [Bindable]  
  4. public var goodsList:ArrayCollection = null;  
  5. protected function goodsList_resultHandler(event:ResultEvent):void{  
  6.     // 获取数据  
  7.     goodsArr = JSON.decode(event.result.toString()) as Array; // 此针对数据类型, 因为我的这个是返回的JSON  
  8.     //将得到的数据用ArrayCollection封装起来  
  9.     goodsList = new ArrayCollection(goodsArr);  
  10.     // 更新dataGrid组件信息  
  11.     goods.dataProvider = goodsList;  
  12. }  
0 0