Flex用JSON处理返回的数据

本文介绍了一种在Flex中利用JSON处理返回数据的方法。通过将原始JSON数据转换为ActionScript对象并使用ArrayCollection作为数据提供者绑定到DataGrid,实现了数据的有效管理和展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
11月27日
Flex用JSON处理返回的数据
FLEX处理返回的数据,然后绑定有很多种方法.不过看牛人们一般都是对数据进行处理成对象,再绑定到数据集.可能是这样更符合面向对象,也更合乎规范。用JSON对那些牛人来讲可能是方便不少,数据条理也相对清晰很多.
<?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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值