flex与c#通讯例子

as代码
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>   
   <![CDATA[
   import mx.rpc.events.FaultEvent;  
    import mx.collections.ArrayCollection;  
    import flash.events.Event;  
    import mx.rpc.events.ResultEvent;  
    import mx.controls.Alert; 
    import mx.events.CloseEvent; 
    import com.adobe.serialization.json.JSON;  
    
    private function personJSON(event:ResultEvent):void  
    {  
        //get the raw JSON data and cast to String  
         
        var rawData:String = String(event.result);  
        //showMsg(rawData); 
        var person = JSON.decode(rawData);  
     
         
        txtName.text = person.data[0].name;  
        txtGx.text = person.data[0].sex;  
        txtAddress.text = person.data[0].age;  
        txtNiceName.text = person.data[0].email; 
        txtEmployees.text = "不是";  

        var persons:Array = person.data as Array;  
        var personsCollection:ArrayCollection = new ArrayCollection(persons);  
        dg.dataProvider = personsCollection;   
    }  
    private function send():void
    {
    //创建一个远程访问对象
    var hs:HTTPService=new HTTPService();
    hs.resultFormat="text";
    //设置请求路径
    hs.url="http://localhost:7242/card_system/test.aspx";
   
    hs.showBusyCursor=true;
    //添加事件机制
    hs.addEventListener(ResultEvent.RESULT,personJSON,false,0,true);
   
   
    //发送请求和参数
    var obj:Object = {YYYY:"2009", MM:"12",DD:'2'};
    hs.send(obj);
    } 
     
    private function showMsg(obj:String):void 
    { 
         var alert:Alert = Alert.show( 
                obj,"Alert Window Title", 
                Alert.OK|Alert.CANCEL|Alert.NO|Alert.YES, 
                this); 
    } 
    
    private function managerJSON(event:ResultEvent):void  
    {  
          
        //获取原始JSON数据并输出到InputText控件上  
        var rawData:String = String(event.result);  
        var alert:Alert = Alert.show( 
                rawData,"Alert Window Title", 
                Alert.OK|Alert.CANCEL|Alert.NO|Alert.YES, 
                this); 
        var manager = JSON.decode(rawData);  
        txtName.text = manager.txt_name;  
        txtGx.text = manager.txt_gx;  
        txtAddress.text = manager.address;  
        txtNiceName.text = manager.nice_name;  
        txtEmployees.text = "是";  
      
        //更新DataGrid  
        var employees:Array = manager.employees as Array;  
        var employeesCollection:ArrayCollection = new ArrayCollection(employees);  
        dg.dataProvider = employeesCollection;  
     }  
     
    private function onSelected():void  
    {  
        txtName.text = dg.selectedItem.txt_name;  
        txtGx.text = dg.selectedItem.txt_gx;  
        txtAddress.text = dg.selectedItem.address;  
        txtNiceName.text = "";  
        txtEmployees.text = "";  
    }  
   ]]>   

  
</mx:Script>   
  <mx:HTTPService id="personRequest" url="http://localhost:7242/card_system/test.aspx"   
    showBusyCursor="true" useProxy="false" method="GET" resultFormat="text"    
    result="personJSON(event)" >   
    <mx:request xmlns="">   
      <data>"true"</data>
      <YYYY>
      2009
      </YYYY> 
    </mx:request>   
  </mx:HTTPService>   
  <mx:HTTPService id="managerRequest" url="http://localhost:7242/card_system/test.aspx"    
    showBusyCursor="true" useProxy="false" method="GET" resultFormat="text"    
    result="managerJSON(event)">   
    <mx:request xmlns="">   
      <getManager>"true"</getManager>   
    </mx:request>   
  </mx:HTTPService>   
    
  <mx:Panel x="0" y="0" width="775" height="248"    
     layout="absolute" title="JSON 数据源实例">   
    <mx:DataGrid id="dg" x="210" y="10" width="505" rowCount="5"   
        rowHeight="25" enabled="true" editable="false" change="onSelected()">    
      <mx:columns>   
        <mx:DataGridColumn headerText="姓名" dataField="name"/>   
        <mx:DataGridColumn headerText="性别" dataField="sex"/>   
        <mx:DataGridColumn headerText="年龄" dataField="age"/>   
        <mx:DataGridColumn headerText="邮箱" dataField="email"/>   
      </mx:columns>   
    </mx:DataGrid>   
      
    <mx:Label x="10" y="12" text="姓名:"/>   
    <mx:TextInput id="txtName" x="50" y="11" width="150" editable="false"/>   
    <mx:Label x="10" y="41" text="关系:"/>   
    <mx:TextInput id="txtGx" x="50" y="40" width="150" editable="false"/>   
    <mx:Label x="10" y="74" text="住址:"/>   
    <mx:TextInput id="txtAddress" x="50" y="72" width="150" editable="false"/>   
    <mx:Label x="10" y="105" text="昵称:"/>   
    <mx:TextInput id="txtNiceName" x="50" y="103" width="150" editable="false"/>   
    <mx:Label x="10" y="136" text="屋主:"/>   
    <mx:TextInput id="txtEmployees" x="50" y="134" width="150"    
        editable="false" text="不是"/>   
      
    <mx:Button id="getPerson" x="270" y="165" label="侄女"    
       click="personRequest.send();"/>   
    <mx:Button id="getManager" x="342.5" y="165" label="我的家族"    
       click="managerRequest.send();"/>   
      <mx:Button id="getPersons" x="444" y="165" label="手工发送请求" click="send();"/>
      
      
  </mx:Panel>   
</mx:Application>
================================
c#代码:

protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "application/json";
      
        Response.Write(getJsonData());
        Response.End();
    }
    private string getJsonData()
    {
        StringBuilder jsonData = new StringBuilder();
        jsonData.Append("{\"data\":[{\"name\":\"hrd888888\",\"sex\":\"男\",\"age\":\"22\",\"email\":\"[email protected]\"}]}");
        //jsonData.Append("{\"nice_name\":\"\u963f\u5065\",\"employees\":[{\"txt_name\":\"\u963f\u96ea\",\"txt_gx\":\"\u8001\u5a46\",\"email\":\"[email protected]\",\"address\":\"\u5e7f\u4e1c\u8087\u5e86\u5e7f\u5b81\u8c2d\u5e03\u6c34\u58f0\u5cad\"},{\"txt_name\":\"\u5b87B\",\"txt_gx\":\"\u513f\u5b50\",\"email\":\"[email protected]\",\"address\":\"\u5730\u7403\u4e9a\u6d32\u4e2d\u56fd\u5e7f\u4e1c\u8087\u5e86\u5e7f\u5b81\u5357\u8857\"}],\"txt_name\":\"\u6b27\u5b97\u5065\",\"txt_gx\":\"\u67f1\u4eba\",\"email\":\"[email protected]\",\"address\":\"\u592a\u9633\u7cfb\u7ecf\u706b\u661f\u5230\u5730\u7403\"}");  
        return jsonData.ToString();
    }  

你可能感兴趣的:(C++,c,json,C#,Flex)