您的位置:首页 > Web前端 > JavaScript

ajax json 数据

2009-01-14 13:52 113 查看
json_ajax_Datable_IList(T).rar

使用datatable作为后台数据源

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

 

<script type="text/javascript">

</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方便
 

handle

加入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;

    }
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: