json_ajax_Datable_IList(T).rar

使用datatable作为后台数据源

例子: 关于JSON的学习/学习json1_datatable.aspx; ToJson.ashx;

 

script>

 

{"Suppliers":[{"SupplierID":"1","CompanyName":"Exotic Liquids","Address":"49 Gilbert St.","City":"London","Country":"UK","Phone":"(171) 555-2222"},{"SupplierID":"2","CompanyName":"New Orleans Cajun Delights","Address":"P.O. Box 78934","City":"New Orleans","Country":"USA","Phone":"(100) 555-4822"},{"SupplierID":"3","CompanyName":"Grandma Kelly's Homestead","Address":"707 Oxford Rd.","City":"Ann Arbor","Country":"USA","Phone":"(313) 555-5735"},{"SupplierID":"4","CompanyName":"Tokyo Traders","Address":"9-8 Sekimai Musashino-shi","City":"Tokyo","Country":"Japan","Phone":"(03) 3555-5011"},{"SupplierID":"5","CompanyName":"Cooperativa de Quesos 'Las Cabras'","Address":"Calle del Rosal 4","City":"Oviedo","Country":"Spain","Phone":"(98) 598 76 54"},
……………………………………

然后用EVAL()把后台传过来的responseText对象转化为js对象

然后用json.Suppliers[index].SupplierID
json.Suppliers[index].CompanyName等访问
比使用Msxml2.DOMDocument控件解析XML方便

 

 ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        SuppliersBLL bll 
= new SuppliersBLL();
        Northwind.SuppliersDataTable table 
= bll.GetSuppliers();
        context.Response.Write(DataTableToJson(table));
        context.Response.End();
        
    }

    
private static string DataTableToJson(Northwind.SuppliersDataTable dt)
    {
        System.Text.StringBuilder jsonBuilder 
= new System.Text.StringBuilder();
        jsonBuilder.Append(
"{\"");
        jsonBuilder.Append(dt.TableName.ToString());
        jsonBuilder.Append(
"\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            jsonBuilder.Append(
"{");
            
for (int j = 0; j < dt.Columns.Count; j++)
            {
                jsonBuilder.Append(
"\"");
                jsonBuilder.Append(dt.Columns[j].ColumnName);
                jsonBuilder.Append(
"\":\"");
                jsonBuilder.Append(dt.Rows[i][j].ToString());
                jsonBuilder.Append(
"\",");
            }
            jsonBuilder.Remove(jsonBuilder.Length 
- 11);
            jsonBuilder.Append(
"},");
        }
        jsonBuilder.Remove(jsonBuilder.Length 
- 11);
        jsonBuilder.Append(
"]");
        jsonBuilder.Append(
"}");
        
return jsonBuilder.ToString();
    }

 

 

使用泛型数组作为后台数据源

关于JSON的学习/使用json_net序列化实体层/Default.aspx; Handler2.ashx;

 


    window.onload=function(){
        xmlhttprequest.open(
"POST","http://www.cnblogs.com/Handler2.ashx",true);
        xmlhttprequest.onreadystatechange
=setSelect;
        xmlhttprequest.send(
null);
    }
    
function setSelect(){
        
if(xmlhttprequest.readyState==4){
            
if(xmlhttprequest.status==200){
                
var son=xmlhttprequest.responseText;
                alert(son);
                
var json=eval("("+son+")");
                alert(json[
0].ID);
                alert(json[
0].CompanyName);
            }
        }
    }
</script>

 

{"ID":1,"CompanyName":"Exotic Liquids"},{"ID":2,"CompanyName":"New Orleans Cajun Delights"},{"ID":3,"CompanyName":"Grandma Kelly's Homestead"},{"ID":4,"CompanyName":"Tokyo Traders"},{"ID":5,"CompanyName":"Cooperativa de Quesos 'Las Cabras'"},{"ID":6,"CompanyName":"Mayumi's"},{"ID":7,"CompanyName":"Pavlova, Ltd."},{"ID":8,"CompanyName":"Specialty Biscuits, Ltd."},{"ID":9,"CompanyName":"PB Knäckebröd AB"},{"ID":10,"CompanyName":"Refrescos Americanas LTDA"},{"ID":11,"CompanyName":"Heli Süßwaren GmbH & Co. KG"},

eval转化为js对象,var json
=eval("("+son+")")
json[
0].ID  //output: 1

 Json.NET。它方便我们读取从浏览器流向服务器的JSON对象,也方便在响应流中写入JSON对象。这里下载:Json.NET

 

加入Newtonsoft.Json DLL
新建一个有supplierID,CompanyName两个字段的model类supplier

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType 
= "text/plain";
        SuppliersBLL bll 
= new SuppliersBLL();
        Northwind.SuppliersDataTable table 
= bll.GetSuppliers();
        IList
<Supplier> lst = reSupplierList(table);
        context.Response.Write(JavaScriptConvert.SerializeObject(lst));
        context.Response.End();
    }
    
public IList<Supplier> reSupplierList(Northwind.SuppliersDataTable table)
    {
        IList
<Supplier> lst = new List<Supplier>();
        
foreach (Northwind.SuppliersRow item in table)
        {
            Supplier model 
= new Supplier();
            model.ID 
= item.SupplierID;
            model.CompanyName 
= item.CompanyName;
            lst.Add(model);
        }
        
return lst;
    }

 

相关文章:

  • 2021-12-16
  • 2021-12-26
  • 2022-03-03
  • 2021-11-27
  • 2021-12-26
  • 2021-12-01
  • 2022-12-23
  • 2022-01-20
猜你喜欢
  • 2022-02-07
  • 2021-11-21
  • 2021-11-27
  • 2022-02-13
  • 2022-02-02
  • 2022-02-01
  • 2021-12-31
相关资源
相似解决方案