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