`

Flex用JSON处理返回的数据

    博客分类:
  • Flex
 
阅读更多

 

 

<?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>  

又或

private function onJSONResult( event:ResultEvent ) : void {
	var data:String = event.result.toString();
	data = data.replace( /\s/g, '' );
	var jd:JSONDecoder = new JSONDecoder( data );
	dg.dataProvider = jd.getValue();
}
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics