HttpService返回數據處理

在調用HttpService返回數據後,我們需要對返回數據進行處理。返回數據的格式resultFormat有幾種類型,object、array、xml、flashvars、text和e4x,默認的設置爲object
看下這幾種類型的原文說明:

object The value returned is XML and is parsed as a tree of ActionScript objects. This is the default. array The value returned is XML and is parsed as a tree of ActionScript objects however if the top level object is not an Array, a new Array is created and the result set as the first item. If makeObjectsBindable is true then the Array will be wrapped in an ArrayCollection. xml The value returned is XML and is returned as literal XML in an ActionScript XMLnode object. flashvars The value returned is text containing name=value pairs separated by ampersands, which is parsed into an ActionScript object. text The value returned is text, and is left raw. e4x The value returned is XML and is returned as literal XML in an ActionScript XML object, which can be accessed using ECMAScript for XML (E4X) expressions.

假設有下面格式的數據:
<dataconfig>
<item>
<prodSpecId id="1">小靈通</prodSpecId>
<orderTypeId id="1">新裝</orderTypeId>
<flowDesc id="1">流程描述</flowDesc>
</item>
<item>
<prodSpecId id="2">固話</prodSpecId>
<orderTypeId id="1">新裝</orderTypeId>
<flowDesc id="2">流程描述</flowDesc>
</item>
<item>
<prodSpecId id="2">固話</prodSpecId>
<orderTypeId id="21">移機</orderTypeId>
<flowDesc id="4">流程描述</flowDesc>
</item>
</dataconfig>

這樣的數據可以直接作爲ComboBox或DataGrid的dataProvider,按如下形式設置:
a).HttpService的resultFormat爲缺省的設置
b).返回值獲取
public function result(data:Object):void{
list = data.result.dataconfig.item;//注意dataconfig.item與xml數據中的對應
}
list可以直接作爲dataPrivoder即可,效果如下圖:

98b6a81f4d8264d1a686693f.jpg

考慮如下樹狀數據:

<?xml version="1.0" encoding="utf-8"?>
<node label="四川省">
<node label="成都市">
<node label="成都市市轄區"/>
<node label="金堂縣"/>
<node label="雙流縣"/>
</node>
<node label="資陽">
<node label="雁江區"/>
<node label="簡陽市"/>
<node label="安嶽縣"/>
<node label="樂至縣"/>
</node>
<node label="自貢市"/>
<node label="攀枝花市"/>
</node>

這樣的數據一般用來作爲Tree控件的dataProvider,但又不能直接像上面一樣設置,需要簡單處理下:
a).HttpService的resultFormat設置爲xml
b).返回數據處理
public function result(data:Object):void{
var xml:XML = <root/>
xml.appendChild(data.result);
areas = xml.node;
}

這裏定義了一個XML對象,將返回值的result添加到XML子節點下,並返回xml對象的node節點,此數據作爲樹的dataProvider,效果如下圖:

67049317a8c80c184b90a739.jpg