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

Flex与服务器通信的常用方式
标签
<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可以取得服务器返回的数据
发送请求,
 jsonService.send();

若有参数
var params:URLVariables=new URLVariables();
params.param1 = param1;  // 直接  .参数 = 参数, 就行了
jsonService.send(param1 );

脚本方式
var jsonService:HTTPService=new HTTPService();
jsonService.url = "http://localhost:8080/getData.json"
jsonService.showBusyCursor=GlobalParamControl.showBusyCursor;
jsonService.addEventListener(ResultEvent.RESULT, ResultFunc); // 省略ResultFunc方法 
// ...其它与上同理


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

以下为Flex的代码:

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

你可能感兴趣的:(java,json,浏览器,Flex,Adobe)