我如何[简单地]在业务线Web应用程序中使用JSON数据

时间:2010-04-21 17:30:59

标签: c# jquery web-services json

我通常使用JSON和jQuery来返回带有html的字符串。但是,我想在我的代码中开始使用Javascript对象。

在我的页面上开始使用json对象的最简单方法是什么?

这是一个示例Ajax调用(当然是$(document).ready( { ... })之后:

 $('#btn').click(function(event) {
     event.preventDefault();
     var out = $('#result');
     $.ajax({ url: "CustomerServices.asmx/GetCustomersByInvoiceCount",
          success: function(msg) {
             // 
             // Iterate through the json results and spit them out to a page?
             // 
          },
                data: "{ 'invoiceCount' : 100 }"
     });
 });

我的WebMethod:

[WebMethod(Description="Gets customers with more than n invoices")]
public List<Customer> GetCustomersByInvoiceCount(int? invoiceCount) {
    using (dbDataContext db = new dbDataContext()) {

        return db.Customers.Where(c => c.InvoiceCount >= invoiceCount);
    }
}

返回的内容:

{"d":[{"__type":"Customer","Account":"1116317","Name":"SOME COMPANY","Address":"UNit 1             , 392 JOHN ST.                            ","LastTransaction":"\/Date(1268294400000)\/","HighestBalance":13922.34},{"__type":"Customer","Account":"1116318","Name":"ANOTHER COMPANY","Address":"UNIT #345             , 392 JOHN ST.                            ","LastTransaction":"\/Date(1265097600000)\/","HighestBalance":549.42}]}

我想知道的是,人们通常用这个返回的json做什么?你是否迭代了属性并动态创建一个html表?

是否可以使用javascript版本的反射(类似.Net GridView控件)来“绑定”JSON数据

您是否将此返回的数据放入Javascript对象中,然后对其执行某些操作?

我想要实现的一个例子就是拥有一个普通的'html页面(在移动设备上),其中包含销售人员的客户列表。单击其中一个客户时,客户ID将发送到Web服务,该服务将检索与销售人员相关的客户详细信息。

我知道SO人才库非常深,所以我认为你们所有人都能够指导正确的方向并给出一些关于最佳方法的想法。

1 个答案:

答案 0 :(得分:3)

我建议您查看各种客户端模板解决方案。甚至还有与下一个jQuery版本here进行集成的讨论。

我更喜欢jTemplate,因为它简单,体积小。但还有其他人。如果您想查看它的运行情况,请查看我们的客户网站here。 我们发现它甚至可以很好地扩展到大量行(> 1000)。

相关问题