我通常将 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(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 控件)“绑定(bind)”JSON 数据
您是否将返回的数据放入 Javascript 对象中,然后对其进行处理?
我想要实现的一个示例是拥有一个普通的 html 页面(在移动设备上),其中包含销售人员的客户列表。当其中一个客户被点击时,客户 ID 会被发送到网络服务,该网络服务会检索与销售人员相关的客户详细信息。
我知道 SO 人才库非常丰富,所以我认为在座的各位能够指引正确的方向,并就实现这一目标的最佳方式给我一些想法。
最佳答案
我建议您查看可用的各种客户端模板解决方案。甚至有人谈论将其与下一个 jQuery 版本集成 here .
我更喜欢 jTemplate因为它的简单和小尺寸。但还有其他人。如果您想实际查看它,请查看我们的客户网站之一 here . 我们发现它甚至可以很好地扩展到大量行(>1000)。
关于c# - 我如何 [简单地] 在业务线 Web 应用程序中使用 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2685163/