flex中使用corelib ActionScript 3 Library sdk包解析json数据
在flex 3中使用 要使用json作为数据交换格式。需引入corelib ActionScript 3 Library 开发包
1. 下载地址:http://www.adobe.com/cfusion/exchange/index.cfm?view=sn111&extid=1078469
2. 下载解压后复制corelib-[1].90\bin 目录下的corelib.swc到你flex 开发包sdk的所在目录。
我的flex sdk是安装在:D:\Program Files\Adobe\Flex Builder 3 Plug-in\
只需将该文件复制到:D:\Program Files\Adobe\Flex Builder 3 Plug-in\sdks\3.0.0\frameworks\libs 下面。
3. 在eclipse下就可以像使用flex 3的标准sdk一样了。import com.adobe.serialization.json.JSON;
注意:该sdk下还有一些其他有用的包,慢慢研究吧。。。。
FLEX处理返回的数据,然后绑定有很多种方法.不过看牛人们一般都是对数据进行处理成对象,再绑定到数据集.可能是这样更符合面向对象,也更合乎规范。用JSON对那些牛人来讲可能是方便不少,数据条理也相对清晰很多.
xml 代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute"
creationComplete="service.send()">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import com.adobe.serialization.json.JSON;
private function onJSONLoad(event:ResultEvent):void
{
//get the raw JSON data and cast to String
var rawData:String = String(event.result);
//decode the data to ActionScript using the JSON API
//in this case, the JSON data is a serialize Array of Objects.
var arr:Array = (JSON.decode(rawData) as Array);
//create a new ArrayCollection passing the de-serialized Array
//ArrayCollections work better as DataProviders, as they can
//be watched for changes.
var dp:ArrayCollection = new ArrayCollection(arr);
//pass the ArrayCollection to the DataGrid as its dataProvider.
grid.dataProvider = dp;
}
]]>
</mx:Script>
<mx:HTTPService id="service" resultFormat="text"
url="http://weblogs.macromedia.com/mesh/mashedpotato.json"
result="onJSONLoad(event)" />
<mx:DataGrid id="grid" right="10" left="10" top="10" bottom="10">
<mx:columns>
<mx:DataGridColumn headerText="Service" dataField="src"/>
<mx:DataGridColumn headerText="Title" dataField="title"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>