• Flex使用JSON格式与Java通信


    Flex使用JSON格式与Java通信

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

    Xml代码 <>
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">  
    3. <mx:Script>  
    4.     <![CDATA[
    5.          import mx.rpc.events.ResultEvent;
    6.          import mx.collections.ArrayCollection;
    7.          import com.adobe.serialization.json.JSON;
    8.          [Bindable]
    9.          private var stuMarkDataArray:ArrayCollection;
    10.         
    11.          private function initApp():void{
    12.              stuMarkDataArray = new ArrayCollection();
    13.          }
    14.          public function sendStuInfo():void{
    15.              var stuFaculty:String = facultyxt.text;
    16.              var stuMajor:String = majortxt.text;
    17.              var stuInfo:Object = new Object;
    18.              stuInfo.faculty = stuFaculty;
    19.              stuInfo.major = stuMajor;
    20.              var stuStr:String = JSON.encode(stuInfo);
    21.              sendInfo.getStuMarks(stuStr);
    22.          }
    23.          public function showStuMark(event:ResultEvent):void{
    24.              note.text = "Success....";
    25.              var rawArray:Array;
    26.              var rawData:String = event.result as String;
    27.              rawArray = JSON.decode(rawData) as Array;
    28.              stuMarkDataArray = new ArrayCollection(rawArray);
    29.          }
    30.          public function getTotalMarks():void
    31.          {
    32.              getTotal.getTotalMarks();
    33.              note.text="I am searching...";
    34.          }
    35.          public function showTotalMark(event:ResultEvent):void{
    36.              note.text = event.result as String;
    37.     
    38.          }
    39.      ]]>  
    40. </mx:Script>  
    41. <mx:RemoteObject id="sendInfo" destination="getStuMark" showBusyCursor="true" result="showStuMark(event)"/>  
    42. <mx:RemoteObject id="getTotal" destination="getTotalMark" showBusyCursor="true" result="showTotalMark(event)"/>  
    43.     <mx:Panel x="39" y="31" width="346" height="301" layout="absolute">  
    44.         <mx:Label x="27" y="25" text="Faculty"/>  
    45.         <mx:TextInput x="72" y="23" id="facultyxt"/>  
    46.         <mx:TextInput x="72" y="64" id="majortxt"/>  
    47.         <mx:Label x="27" y="66" text="Major"/>  
    48.         <mx:Button x="27" y="92" label="各科成绩" width="109" click="sendStuInfo()"/>  
    49.         <mx:DataGrid x="10" y="140" height="79" width="304" dataProvider="{stuMarkDataArray}">  
    50.             <mx:columns>  
    51.                 <mx:DataGridColumn headerText="Math" dataField="math"/>  
    52.                 <mx:DataGridColumn headerText="Chinese" dataField="chn"/>  
    53.                 <mx:DataGridColumn headerText="English" dataField="eng"/>  
    54.             </mx:columns>  
    55.         </mx:DataGrid>  
    56.         <mx:Label x="10" y="227" width="304" id="note" height="24"/>  
    57.         <mx:Button x="192" y="94" label="总成绩" click="getTotalMarks()"/>  
    58.     </mx:Panel>  
    59.       
    60. </mx:Application>  
    <?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

    Java代码
    1. package test;  
    2. import net.sf.json.JSONArray;  
    3. import net.sf.json.JSONObject;  
    4. public class JsonTestJava {  
    5.   public String getStuMarks(String info){  
    6.        String result = new String();  
    7.        JSONObject jsonObject = JSONObject.fromObject(info);  
    8.        String faculty = jsonObject.getString("faculty");  
    9.        String major = jsonObject.getString("major");  
    10.         
    11.         JSONArray stuMarksArray = new JSONArray();    
    12.            
    13.      if(faculty.equals("IT") && major.equals("SE")){  
    14.         for(int i=0;i<3;i++){  
    15.              JSONObject stuObj = new JSONObject();   
    16.              stuObj.put("math", 100-i*2);  
    17.              stuObj.put("eng", 80+2*i);  
    18.              stuObj.put("chn", 90-i);  
    19.              stuMarksArray.add(stuObj);  
    20.          }  
    21.       }  
    22.       result = stuMarksArray.toString();  
    23.       return result;  
    24.    }  
    25.   public String getTotalMarks(){  
    26.        JSONObject stuObj = new JSONObject();   
    27.        stuObj.put("name", "leyond");  
    28.        stuObj.put("mark","344");  
    29.       return stuObj.toString();  
    30.    }  
    31. }  
    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的数据,请问如何在前台解析出来的。就是这里的代码该如何写呢:

    Js代码
    1. public function showTotalMark(event:ResultEvent):void{  
    2.     var marks:Object= JSON.decode(event.result as String);  
    3.      note.text = marks.name + marks.mark;      
    4. }  
    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:

  • 相关阅读:
    页面整体布局思路
    CSS3转换、过渡、动画效果及css盒子模型
    CSS随笔
    CSS基础,认识css样式
    HTML基础表单
    HTML基础
    java.sql.SQLException: 调用中无效的参数DSRA0010E: SQL 状态 = null,错误代码 = 17,433
    There is no Action mapped for namespace / and action name accredit.
    myeclipse开启后卡死、building workspace缓慢 问题解决
    you need to upgrade the working copy first
  • 原文地址:https://www.cnblogs.com/danghuijian/p/4400667.html
Copyright © 2020-2023  润新知