1、需要在flex的SDK中加入as3corelib.swc(该文件250K,网上有下载,如果下载下来的没有250k,不能使用),以我本机为例,将该文件放到:D:\Adobe\Flex Builder 3 Plug-in\sdks\3.2.0\frameworks\libs文件夹下,重新打开编辑器。
2、使用时需要引入import com.adobe.serialization.json.*;
3、但是对JSON有严格的格式要求,即属性和值都要有双引号,否则解析不出来。
4、这里简单给出使用的例子:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" height="534" width="1122">
<mx:Script>
<![CDATA[
import com.adobe.serialization.json.JSON;
import mx.collections.ArrayCollection;
public var rawData:String =
'[{"name":"stone","sex":"男","teleNum":"15903396795"},' +
'{"name":"huangyunzeng","sex":"男","teleNum":"15903396795"},' +
'{"name":"huangyz","sex":"男","teleNum":"15903396795"}]';
public var arr:Array = (JSON.decode(rawData) as Array);
[Bindable]
public var dp:ArrayCollection = new ArrayCollection(arr);
]]>
</mx:Script>
<mx:RemoteObject id="firstRO" destination="testHello"/>
<mx:Panel width="702" height="280" layout="absolute" title="我的面板" x="236" y="86">
<mx:DataGrid x="161" y="27" height="156" dataProvider="{dp}">
<mx:columns>
<mx:DataGridColumn headerText="姓名" dataField="name"/>
<mx:DataGridColumn headerText="性别" dataField="sex"/>
<mx:DataGridColumn headerText="电话号码" dataField="teleNum"/>
</mx:columns>
</mx:DataGrid>
</mx:Panel>
</mx:Application>