0

我的 jquery ajax 调用了我的 codebehining 静态 WebMethod,我曾经 System.Web.Script.Serialization.JavaScriptSerializer获取 JSON 对象,输出是这样的。

[{"ProductId":"9","Category":"TV","Products":"Discovery","Price":15.97},{"ProductId":"25","Category":"TV","Products":"HBO","Price":15.97}]

我有具有 ProductId、Category、Products、Price 列的 GridView(asp.net 控件)。我应该使用 JavaScript 将此 json 对象绑定到 gridview。

我什至不知道如何在上面的 Json 字符串上申请循环。请对此有所了解。

4

3 回答 3

1

You can loop thru your JSON data like this

var data=[{"ProductId":"9","Category":"TV","Products":"Discovery","Price":15.97},
          {"ProductId":"25","Category":"TV","Products":"HBO","Price":15.97}];

$.each(data,function(index,item){
    alert(item.ProductId);
    alert(item.Category);
}); 

To replace the Grid, You can build the HTML markup for a table and inject that to the DOM.

 var itemRow="<table>";  
 $.each(data,function(index,item){
    itemRow+="<tr><td>"+item.ProductId+"</td><td>"+item.Category+"</td></tr>";
 });        
 itemRow+="</table>";  

 $("#divItems").html(itemRow);

Working sample http://jsfiddle.net/qS7uD/6/

But you will not get the ASP.NET Grid Events after this as it is pure HTML markup for the display

于 2012-07-09T17:05:53.973 回答
0

要反序列化您的 JSON,您应该创建一个具有相同属性的类并像这样使用 JavascriptSerilzier。

public class Product
{
  public int ProductId{get;set;}
  public string Category{get;set;} 
  public string Products{get;set;} 
  public decimal Price{get;set;}
} 


myProducts List<Product> = new JavaScriptSerializer().Deserialize<List<Project>>(myJson);

然后,您可以使用简单的 for-each 循环遍历您的产品列表。您也可以使用 List 作为数据源来绑定您的控件。

于 2012-07-09T17:01:10.237 回答
0

您不能使用默认的 asp.net 网格来执行此操作,请查看 jQGrid以获取基于 json 的网格

于 2012-07-09T16:46:15.307 回答