asp.netjson:Flash ActionScript 3.0(3) - 以文本形式、XML形式和JSON形式和ASP.NET通信

  本文举例源代码或素材下载

  举例

  Text.aspx.cs

using ;
using .Data;
using .Configuration;
using .Collections;
using .Web;
using .Web.Security;
using .Web.UI;
using .Web.UI.WebControls;
using .Web.UI.WebControls.WebParts;
using .Web.UI.HtmlControls;
  
public partial Text : .Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
     s = "name: " + Request.QueryString["name"] + "; age: " + Request.QueryString["age"];
  
    Response.ClearContent;
    Response.ContentType = "text/plain";
    Response.Write(s);
    Response.End;
  }
}


  Xml.aspx.cs

using ;
using .Data;
using .Configuration;
using .Collections;
using .Web;
using .Web.Security;
using .Web.UI;
using .Web.UI.WebControls;
using .Web.UI.WebControls.WebParts;
using .Web.UI.HtmlControls;
  
public partial Xml : .Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
     s = @"<?xml version=""1.0"" encoding=""utf-8""?>
      <root>
        <person name=""webabcd"" age=""27"">
          <salary>1000</salary>
        </person>
        <person name=""webabcdefg"" age=""37"">
          <salary>2000</salary>
        </person>
        <person name=""webabcdefghijklmn"" age=""47"">
          <salary>3000</salary>
        </person>
      </root>";
  
    Response.ClearContent;
    Response.ContentType = "text/xml";
    Response.Write(s);
    Response.End;
  }
}


  JSON.aspx.cs

using ;
using .Data;
using .Configuration;
using .Collections;
using .Web;
using .Web.Security;
using .Web.UI;
using .Web.UI.WebControls;
using .Web.UI.WebControls.WebParts;
using .Web.UI.HtmlControls;
  
public partial JSON : .Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    Person person = Person;
    person.Name = "webabcd";
    person.Age = 27;
  
    HttpContext.Current.Response.ClearContent;
    // HttpContext.Current.Response.ContentType = "application/json";
    HttpContext.Current.Response.ContentType = "text/plain";
  
    // 把person对象序列化成JSON
    .Runtime.Serialization.DataContractJsonSerializer dcjs = .Runtime.Serialization.DataContractJsonSerializer(person.GetType);
    dcjs.WriteObject(HttpContext.Current.Response.OutputStream, person);
  
    HttpContext.Current.Response.End;
  }
}
  
/**//// <summary>
/// Person类
/// </summary>
[.Runtime.Serialization.DataContract]
public Person
{
  private _name;
  /**//// <summary>
  /// 姓名
  /// </summary>
  [.Runtime.Serialization.DataMember]
  public Name
  {
    get { _name; }
     { _name = value; }
  }
  
  private _age;
  /**//// <summary>
  /// 年龄
  /// </summary>
  [.Runtime.Serialization.DataMember]
  public Age
  {
    get { _age; }
     { _age = value; }
  }
}


  Net.as

package
{
  import flash.display.Sprite;
  import flash.net.URLLoader;
  import flash.net.URLRequest;
  import flash.net.URLVariables;
  import flash.net.URLRequestMethod;
  import flash.events.Event;
  
  // 对JSON支持
  import com.adobe.serialization.json.JSON;
  
  public Net extends Sprite
  {
    public function Net
    {
      // 以文本形式和ASP.NET通信
      showText;
      
      // 以XML形式和ASP.NET通信
      showXml;
      
      // 以JSON形式和ASP.NET通信
      showJSON;
    }
    
    // 以文本形式和ASP.NET通信
    function showText:void
    {
      var v:URLVariables = URLVariables("name=webabcd&age=27");
      var r:URLRequest = URLRequest;
      r.url = "http://localhost:1343/Web/Text.aspx";
      r.method = URLRequestMethod.GET;
      r.data = v;
      
      var l:URLLoader = URLLoader;
      l.load(r);
      l.addEventListener(Event.COMPLETE, textCompleteHandler);
    }
    
    function textCompleteHandler(event:Event):void
    {
      var l:URLLoader = URLLoader(event.target);
      
      trace(l.data);
      // output: name: webabcd; age: 27
    }
    
    // 以XML形式和ASP.NET通信
    function showXml:void
    {
      var v:URLVariables = URLVariables
      var r:URLRequest = URLRequest;
      r.url = "http://localhost:1343/Web/Xml.aspx";
      r.method = URLRequestMethod.GET;
      r.data = v;
      
      
      var l:URLLoader = URLLoader;
      l.load(r);
      l.addEventListener(Event.COMPLETE, xmlCompleteHandler);
    }
    
    function xmlCompleteHandler(event:Event):void
    {
      var l:URLLoader = event.target as URLLoader;
      var xml:XML = XML(l.data);
      
      for each(var v in xml.person)
      {
        trace("姓名:" + v.@name + ";年龄:" + v.@age + ";薪水:" + v.salary);
      }
      // output:
      // 姓名:webabcd;年龄:27;薪水:1000
      // 姓名:webabcdefg;年龄:37;薪水:2000
      // 姓名:webabcdefghijklmn;年龄:47;薪水:30
    }
    
    // 以JSON形式和ASP.NET通信
    function showJSON:void
    {
      var v:URLVariables = URLVariables
      var r:URLRequest = URLRequest;
      r.url = "http://localhost:1343/Web/JSON.aspx";
      r.method = URLRequestMethod.GET;
      r.data = v;
      
      
      var l:URLLoader = URLLoader;
      l.load(r);
      l.addEventListener(Event.COMPLETE, jsonCompleteHandler);
    }
    
    function jsonCompleteHandler(event:Event):void
    {
      var l:URLLoader = event.target as URLLoader;
      
      var v:* = JSON.decode(l.data);
      
      trace("姓名:" + v.Name + ";年龄:" + v.Age);
      // output: 姓名:webabcd;年龄:27
    }
  }
}




  OK

  相关文章:

  Flash ActionScript 3.0(1) - 数据类型、操作符和流程控制语句

  Flash ActionScript 3.0(2) - 包、类、包外类、命名空间、属性、思路方法、接口和继承



Tags:  jqueryjson flashjson extjsonasp.net asp.netjson

延伸阅读

最新评论

发表评论