Flex使用JSON格式与Java通信

使用Flex,Java,Json更新Mysql数据【高级篇】 中我记得Flex界面中数据是使用一个JsonArray封装了JsonObject之后传给Java后台的。可是今天发现,如果你的数据只有一个JsonObject,那么是否直接传JsonObject也可以呢?试试看:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">
<mx:Script>
	<![CDATA[
		import mx.rpc.events.ResultEvent; 
        import mx.collections.ArrayCollection; 
        import com.adobe.serialization.json.JSON;
        [Bindable]
        private var stuMarkDataArray:ArrayCollection;
        
        private function initApp():void{
        	stuMarkDataArray = new ArrayCollection();
        }
        public function sendStuInfo():void{
        	var stuFaculty:String = facultyxt.text;
        	var stuMajor:String = majortxt.text;
        	var stuInfo:Object = new Object;
        	stuInfo.faculty = stuFaculty;
        	stuInfo.major = stuMajor;
        	var stuStr:String = JSON.encode(stuInfo);
        	sendInfo.getStuMarks(stuStr);
        }
        public function showStuMark(event:ResultEvent):void{
        	note.text = "Success....";
        	var rawArray:Array;
        	var rawData:String = event.result as String;
        	rawArray = JSON.decode(rawData) as Array;
        	stuMarkDataArray = new ArrayCollection(rawArray);
        }
        public function getTotalMarks():void
        {
        	getTotal.getTotalMarks();
        	note.text="I am searching...";
        }
        public function showTotalMark(event:ResultEvent):void{
        	note.text = event.result as String;
    
        }
	]]>
</mx:Script>
<mx:RemoteObject id="sendInfo" destination="getStuMark" showBusyCursor="true" result="showStuMark(event)"/>
<mx:RemoteObject id="getTotal" destination="getTotalMark" showBusyCursor="true" result="showTotalMark(event)"/>
	<mx:Panel x="39" y="31" width="346" height="301" layout="absolute">
		<mx:Label x="27" y="25" text="Faculty"/>
		<mx:TextInput x="72" y="23" id="facultyxt"/>
		<mx:TextInput x="72" y="64" id="majortxt"/>
		<mx:Label x="27" y="66" text="Major"/>
		<mx:Button x="27" y="92" label="各科成绩" width="109" click="sendStuInfo()"/>
		<mx:DataGrid x="10" y="140" height="79" width="304" dataProvider="{stuMarkDataArray}">
			<mx:columns>
				<mx:DataGridColumn headerText="Math" dataField="math"/>
				<mx:DataGridColumn headerText="Chinese" dataField="chn"/>
				<mx:DataGridColumn headerText="English" dataField="eng"/>
			</mx:columns>
		</mx:DataGrid>
		<mx:Label x="10" y="227" width="304" id="note" height="24"/>
		<mx:Button x="192" y="94" label="总成绩" click="getTotalMarks()"/>
	</mx:Panel>
	
</mx:Application>

 后台JSonTestJava.java

package test;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonTestJava {
  public String getStuMarks(String info){
	  String result = new String();
	  JSONObject jsonObject = JSONObject.fromObject(info);
	  String faculty = jsonObject.getString("faculty");
	  String major = jsonObject.getString("major");
	  
	   JSONArray stuMarksArray = new JSONArray();  
	     
	 if(faculty.equals("IT") && major.equals("SE")){
		for(int i=0;i<3;i++){
			JSONObject stuObj = new JSONObject(); 
			stuObj.put("math", 100-i*2);
			stuObj.put("eng", 80+2*i);
			stuObj.put("chn", 90-i);
			stuMarksArray.add(stuObj);
		}
	 }
	 result = stuMarksArray.toString();
	  return result;
  }
  public String getTotalMarks(){
	  JSONObject stuObj = new JSONObject(); 
	  stuObj.put("name", "leyond");
	  stuObj.put("mark","344");
	  return stuObj.toString();
  }
}

 使用的方法跟JSONArray差不多,只不过使用JsonArray的时候,需要先把每个jsonObject解析出来。所以比较方便的。但是现在有一个问题,如果你在后台传给的前台Flex中的是一个JSONObject的数据,请问如何在前台解析出来的。就是这里的代码该如何写呢:

        public function showTotalMark(event:ResultEvent):void{
        	var marks:Object= JSON.decode(event.result as String);
        	note.text = marks.name + marks.mark;    
        }

 这样就可以了,marks.name 和marks.mark就可以分别解析出name和mark。是不是很方便呢。当然如果你的数据很多,就需要用到JSAONArray了。这里就不多讲了。这个是这个例子的效果实例:

Reference: http://giantflyingsaucer.com/blog/?p=10&cpage=1

你可能感兴趣的:(java,spring,json,Flex,Adobe)