jQuery备忘之:jQuery处理.Net后台返回的Xml格式与Json格式的数据的比较分析

最近项目空闲下来,手头也没有什么事情,所以研究了一下jQuery对后台返回数据格式的处理。可能也是因为之前用JS写code的习惯,所以比较倾向于用JS处理XML格式的数据,当然这也就会造成我后台返回的数据的格式全部为XML的格式,处理起来相对繁琐。

      当然我也早就发现Jquery与Json格式的数据结合的非常好,但是一直苦于.NET的对数据转换成Json格式的繁琐,所以一直也没有采用Json格式的数据,或者是在数据相对简单的情况下采用Json格式的数据,其他千篇一律XML,但是今天我分析了一下Jquery对两种数据格式的处理之后,完全改变了心态,并力推大家用Jquery + Json处理后台返回数据。

      请看如下比较:

     【Jquery + Xml】

      如下代码是我查询IPInfo表中前10条数据,并以XML格式输出:

ExpandedBlockStart.gif 代码
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--&gt public   void  XmlToAjax()
{
   DataTable dt 
=  SQLHelper.ExecuteDataTable(SQLHelper.conStrings, CommandType.Text,  " select top(10) * from IPInfo " );
   DataSet ds 
=   new  DataSet();
   ds.Tables.Add(dt);
   
// Console
   Response.ContentType  =   " text/xml " ;
   Response.Charset 
=   " UTF-8 " ;
   Response.Clear();
   Response.Write(
" <?xml  version=\ " 1.0 \ "  encoding=\ " utf - 8 \ "  ?>\n  "   +  ds.GetXml());
   Response.End();
}

 

     我在前台接收到数据后进行如下处理:

ExpandedBlockStart.gif 代码
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--&gt function  checkText() {
            
var  xmlT;
            
if  ($.trim($( " #txtID " ).val())  ==   "" ) {
                $(
" #sm " ).text( " 请输入XML条件 " );
                $(
" #txtID " ).focus();
                
return   false ;
            }
            
else  {
                $(
" #sm " ).text( "" );
                $.ajax({
                    type: 
" GET " ,  
                  
// contentType: "application/json", //WebService 会返回Json类型
                    url:  " ../Ajax/Ajax1.aspx "
                    dataType: 
" xml " ,
                    data: 
"" ,   // 这里是要传递的参数,格式为 data: "{paraName:paraValue}",下面将会看到       
                    success:  function (result) {      // 回调函数,result,返回值
                         var  ID  =   '' ;
                        $(result).find(
" Table1 " ).each( function () {
                            ID 
+=  $( this ).children( " IPid " ).text()  +   "
" ;
                        });
                        $(
" #divMsg " ).html(ID);
                    }
                });
            }

        }

 

     下面是输出结果:

     result.xml 的结果集为:

 

ExpandedBlockStart.gif 代码
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--&gt result.xml
"
xml version="1.0" ?>
< NewDataSet >
    
< Table1 >
        
< IPid > 1 IPid >
        
< IPFrom > 0.0.0.0         IPFrom >
        
< IPTo > 0.255.255.255   IPTo >
        
< IPLocation > IANA  CZ88.NET IPLocation >
        
< IPToNumber > 16777215 IPToNumber >
        
< IPFromNumber > 0 IPFromNumber >
    
Table1 >
    
< Table1 >
        
< IPid > 2 IPid >
        
< IPFrom > 1.0.0.0         IPFrom >
        
< IPTo > 1.255.255.255   IPTo >
        
< IPLocation > IANA  CZ88.NET IPLocation >
        
< IPToNumber > 33554431 IPToNumber >
        
< IPFromNumber > 16777216 IPFromNumber >
    
Table1 >
    
< Table1 >
        
< IPid > 3 IPid >
        
< IPFrom > 2.0.0.0         IPFrom >
        
< IPTo > 2.255.255.255   IPTo >
        
< IPLocation > IANA  CZ88.NET IPLocation >
        
< IPToNumber > 50331647 IPToNumber >
        
< IPFromNumber > 33554432 IPFromNumber >
    
Table1 >
    
< Table1 >
        
< IPid > 4 IPid >
        
< IPFrom > 3.0.0.0         IPFrom >
        
< IPTo > 3.255.255.255   IPTo >
        
< IPLocation > 美国 新泽西通用电气公司 IPLocation >
        
< IPToNumber > 67108863 IPToNumber >
        
< IPFromNumber > 50331648 IPFromNumber >
    
Table1 >
    
< Table1 >
        
< IPid > 5 IPid >
        
< IPFrom > 4.0.0.0         IPFrom >
        
< IPTo > 4.9.255.255     IPTo >
        
< IPLocation > 美国  CZ88.NET IPLocation >
        
< IPToNumber > 67764223 IPToNumber >
        
< IPFromNumber > 67108864 IPFromNumber >
    
Table1 >
    
< Table1 >
        
< IPid > 6 IPid >
        
< IPFrom > 4.10.0.0        IPFrom >
        
< IPTo > 4.10.255.255    IPTo >
        
< IPLocation > 美国 新泽西州立大学 IPLocation >
        
< IPToNumber > 67829759 IPToNumber >
        
< IPFromNumber > 67764224 IPFromNumber >
    
Table1 >
    
< Table1 >
        
< IPid > 7 IPid >
        
< IPFrom > 4.11.0.0        IPFrom >
        
< IPTo > 4.11.255.255    IPTo >
        
< IPLocation > 美国 夏威夷 IPLocation >
        
< IPToNumber > 67895295 IPToNumber >
        
< IPFromNumber > 67829760 IPFromNumber >
    
Table1 >
    
< Table1 >
        
< IPid > 8 IPid >
        
< IPFrom > 4.12.0.0        IPFrom >
        
< IPTo > 4.19.77.255     IPTo >
        
< IPLocation > 美国  CZ88.NET IPLocation >
        
< IPToNumber > 68374015 IPToNumber >
        
< IPFromNumber > 67895296 IPFromNumber >
    
Table1 >
    
< Table1 >
        
< IPid > 9 IPid >
        
< IPFrom > 4.19.78.0       IPFrom >
        
< IPTo > 4.19.78.255     IPTo >
        
< IPLocation > 美国 西南政法大学 IPLocation >
        
< IPToNumber > 68374271 IPToNumber >
        
< IPFromNumber > 68374016 IPFromNumber >
    
Table1 >
    
< Table1 >
        
< IPid > 10 IPid >
        
< IPFrom > 4.19.79.0       IPFrom >
        
< IPTo > 4.19.79.63      IPTo >
        
< IPLocation > 美国 Armed Forces Radio/Television IPLocation >
        
< IPToNumber > 68374335 IPToNumber >
        
< IPFromNumber > 68374272 IPFromNumber >
    
Table1 >
NewDataSet >
"

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-663227/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12639172/viewspace-663227/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值