积少成多Flash(3) - ActionScript 3.0 基础之以文本形式、XML形式和JSON形式与ASP.NET通信

简介:
索引页]
[源码下载]


积少成多Flash(3) - ActionScript 3.0 基础之以文本形式、XML形式和JSON形式与ASP.NET通信


作者: webabcd


介绍
Flash ActionScript 3.0  以文本形式与ASP.NET通信、以XML形式与ASP.NET通信和以JSON形式与ASP.NET通信


示例
Text.aspx.cs
InBlock.gif using System; 
InBlock.gif using System.Data; 
InBlock.gif using System.Configuration; 
InBlock.gif using System.Collections; 
InBlock.gif using System.Web; 
InBlock.gif using System.Web.Security; 
InBlock.gif using System.Web.UI; 
InBlock.gif using System.Web.UI.WebControls; 
InBlock.gif using System.Web.UI.WebControls.WebParts; 
InBlock.gif using System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gif public partial  class Text : System.Web.UI.Page 
InBlock.gif
InBlock.gif         protected  void Page_Load( object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                 string s =  "name: " + Request.QueryString[ "name"] +  "; age: " + Request.QueryString[ "age"]; 
InBlock.gif 
InBlock.gif                Response.ClearContent(); 
InBlock.gif                Response.ContentType =  "text/plain"
InBlock.gif                Response.Write(s); 
InBlock.gif                Response.End(); 
InBlock.gif        } 
InBlock.gif}
 
Xml.aspx.cs
InBlock.gif using System; 
InBlock.gif using System.Data; 
InBlock.gif using System.Configuration; 
InBlock.gif using System.Collections; 
InBlock.gif using System.Web; 
InBlock.gif using System.Web.Security; 
InBlock.gif using System.Web.UI; 
InBlock.gif using System.Web.UI.WebControls; 
InBlock.gif using System.Web.UI.WebControls.WebParts; 
InBlock.gif using System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gif public partial  class Xml : System.Web.UI.Page 
InBlock.gif
InBlock.gif         protected  void Page_Load( object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                 string s =  @"<?xml version=""1.0 "" encoding=""utf-8""?> 
InBlock.gif                        <root> 
InBlock.gif                                <person name= ""webabcd"" age= ""27""> 
InBlock.gif                                        <salary>1000</salary> 
InBlock.gif                                </person> 
InBlock.gif                                <person name= ""webabcdefg"" age= ""37""> 
InBlock.gif                                        <salary>2000</salary> 
InBlock.gif                                </person> 
InBlock.gif                                <person name= ""webabcdefghijklmn"" age= ""47""> 
InBlock.gif                                        <salary>3000</salary> 
InBlock.gif                                </person> 
InBlock.gif                        </root>"; 
InBlock.gif 
InBlock.gif                Response.ClearContent(); 
InBlock.gif                Response.ContentType =  "text/xml"
InBlock.gif                Response.Write(s); 
InBlock.gif                Response.End(); 
InBlock.gif        } 
InBlock.gif}
 
JSON.aspx.cs
InBlock.gif using System; 
InBlock.gif using System.Data; 
InBlock.gif using System.Configuration; 
InBlock.gif using System.Collections; 
InBlock.gif using System.Web; 
InBlock.gif using System.Web.Security; 
InBlock.gif using System.Web.UI; 
InBlock.gif using System.Web.UI.WebControls; 
InBlock.gif using System.Web.UI.WebControls.WebParts; 
InBlock.gif using System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gif public partial  class JSON : System.Web.UI.Page 
InBlock.gif
InBlock.gif         protected  void Page_Load( object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                Person person =  new Person(); 
InBlock.gif                person.Name =  "webabcd"
InBlock.gif                person.Age = 27; 
InBlock.gif 
InBlock.gif                HttpContext.Current.Response.ClearContent(); 
InBlock.gif                 // HttpContext.Current.Response.ContentType = "application/json"; 
InBlock.gif                HttpContext.Current.Response.ContentType =  "text/plain"
InBlock.gif 
InBlock.gif                 // 把person对象序列化成JSON 
InBlock.gif                System.Runtime.Serialization.DataContractJsonSerializer dcjs =  new System.Runtime.Serialization.DataContractJsonSerializer(person.GetType()); 
InBlock.gif                dcjs.WriteObject(HttpContext.Current.Response.OutputStream, person); 
InBlock.gif 
InBlock.gif                HttpContext.Current.Response.End(); 
InBlock.gif        } 
InBlock.gif
InBlock.gif 
/// <summary> 
/// Person类 
/// </summary> 
InBlock.gif[System.Runtime.Serialization.DataContract] 
InBlock.gif public  class Person 
InBlock.gif
InBlock.gif         private  string _name; 
InBlock.gif         /// <summary> 
InBlock.gif         /// 姓名 
InBlock.gif         /// </summary> 
InBlock.gif        [System.Runtime.Serialization.DataMember] 
InBlock.gif         public  string Name 
InBlock.gif        { 
InBlock.gif                get {  return _name; } 
InBlock.gif                set { _name = value; } 
InBlock.gif        } 
InBlock.gif 
InBlock.gif         private  int _age; 
InBlock.gif         /// <summary> 
InBlock.gif         /// 年龄 
InBlock.gif         /// </summary> 
InBlock.gif        [System.Runtime.Serialization.DataMember] 
InBlock.gif         public  int Age 
InBlock.gif        { 
InBlock.gif                get {  return _age; } 
InBlock.gif                set { _age = value; } 
InBlock.gif        } 
InBlock.gif}
 
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 class 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 = new URLVariables("name=webabcd&age=27"); 
                        var r:URLRequest = new URLRequest(); 
                        r.url = "http://localhost:1343/Web/Text.aspx"; 
                        r.method = URLRequestMethod.GET; 
                        r.data = v; 
                         
                        var l:URLLoader = new 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 = new URLVariables() 
                        var r:URLRequest = new URLRequest(); 
                        r.url = "http://localhost:1343/Web/Xml.aspx"; 
                        r.method = URLRequestMethod.GET; 
                        r.data = v; 
                         
                         
                        var l:URLLoader = new URLLoader(); 
                        l.load(r); 
                        l.addEventListener(Event.COMPLETE, xmlCompleteHandler); 
                } 
                 
                function xmlCompleteHandler(event:Event):void 
                { 
                        var l:URLLoader = event.target as URLLoader; 
                        var xml:XML = new 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 = new URLVariables() 
                        var r:URLRequest = new URLRequest(); 
                        r.url = "http://localhost:1343/Web/JSON.aspx"; 
                        r.method = URLRequestMethod.GET; 
                        r.data = v; 
                         
                         
                        var l:URLLoader = new 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 
                } 
        } 
}
 
 




     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/342184 ,如需转载请自行联系原作者

相关文章
|
3月前
|
XML 存储 JSON
Python学习 -- 常用数据交换格式(CSV、XML、JSON)
Python学习 -- 常用数据交换格式(CSV、XML、JSON)
31 0
|
3月前
|
XML JSON 数据格式
xml与JSON的区别
xml与JSON的区别
30 1
|
2月前
|
XML 机器学习/深度学习 JSON
在火狐浏览器调ajax获取json数据时,控制台提示“XML 解析错误:格式不佳”。
在火狐浏览器调ajax获取json数据时,控制台提示“XML 解析错误:格式不佳”。
29 0
在火狐浏览器调ajax获取json数据时,控制台提示“XML 解析错误:格式不佳”。
|
16天前
|
存储 JSON 数据挖掘
python逐行读取txt文本中的json数据,并进行处理
Python代码示例演示了如何读取txt文件中的JSON数据并处理。首先,逐行打开文件,然后使用`json.loads()`解析每一行。接着,处理JSON数据,如打印特定字段`name`。异常处理包括捕获`JSONDecodeError`和`KeyError`,确保数据有效性和字段完整性。将`data.txt`替换为实际文件路径运行示例。
14 2
|
21天前
|
XML JSON JavaScript
使用JSON和XML:数据交换格式在Java Web开发中的应用
【4月更文挑战第3天】本文比较了JSON和XML在Java Web开发中的应用。JSON是一种轻量级、易读的数据交换格式,适合快速解析和节省空间,常用于API和Web服务。XML则提供更强的灵活性和数据描述能力,适合复杂数据结构。Java有Jackson和Gson等库处理JSON,JAXB和DOM/SAX处理XML。选择格式需根据应用场景和需求。
|
3月前
|
XML 存储 JSON
详细比较JSON和XML这两种数据格式
详细比较JSON和XML这两种数据格式
106 2
|
4月前
|
SQL JSON 关系型数据库
【SQL编程】MySQL 5.7.28 版本使用 SQL 直接解析 JSON 字符串(判断是否是合法JSON类型+文本深度+文本长度+值类型+keys获取+值获取+不同深度数据获取)
【SQL编程】MySQL 5.7.28 版本使用 SQL 直接解析 JSON 字符串(判断是否是合法JSON类型+文本深度+文本长度+值类型+keys获取+值获取+不同深度数据获取)
51 0
|
4月前
|
XML 存储 JSON
实现XML与JSON转换,实测这个方法最便捷
XML和JSON是当今最常用的两种数据格式,在数据交换和存储领域占有重要地位。XML以其树状结构和可扩展性被广泛使用,而JSON则以其轻量级和易读性受到开发者的青睐。有时候,为了更好地利用它们的优点解决数据共享、数据处理和数据存储等问题,我们需要将这两种格式进行转换。本文将介绍如何使用Java实现将XML格式转换成JSON格式。
|
4月前
|
XML 存储 JSON
C# 对象存储 (轻松实现序列化 | Xml | Json | 加密 | 压缩 | 注册表 | Redis)
开发时经常会遇到需要保存配置的情况,最常见的实现方式是将对象序列化成Json,再写入文件并保存到本地磁盘。 本文将使用开源库**ApeFree.DataStore**来替换原有的对象存储过程,实现一个可以随意切换存储方式的对象存储方法。 ApeFree.DataStore是一款可配置的对象存储库,支持在不同平台/介质中对内存中的对象进行存储与还原(如本地存储、注册表存储)。支持配置序列化格式(如Json、Xml),支持配置压缩算法(如GZip、Defalte),支持配置加密算法(如AES、RSA)。
68 0
C# 对象存储 (轻松实现序列化 | Xml | Json | 加密 | 压缩 | 注册表 | Redis)