首页 > 解决方案 > 如何在asp.net的gridview中添加嵌套的json数组?

问题描述

我想将嵌套的 JSON 数组放在 asp.net 的 gridview 中。我正在添加我的嵌套 JSON 数组代码。

 [
   {
        "id": 0,
        "orderNumber": "ORD506072018",
        "orderDate": "7/6/2018 12:00:00 AM",
        "customerId": 0,
        "totalAmount": 0,
        "status": null,
        "productId": 0,
        "brandId": 0,
        "Product": [
            {
                "id": 0,
                "productId": 0,
                "quantity": 1,
                "productName": "Maida",
                "brandName": "Kadaksing Masale",
                "productCode": 0,
                "brandId": 0,
                "productPrice": 0
            }
        ],
        "objProduct": {
            "id": 0,
            "productId": 0,
            "quantity": 1,
            "productName": "Rayta",
            "brandName": "Kadaksing Masale",
            "productCode": 0,
            "brandId": 0,
            "productPrice": 0
        },
        "brandName": null,
        "unitId": 0,
        "quantity": 0,
        "pricePerUnit": 0,
        "orderId": 11,
        "eachItemTotalPrice": 0
    }
]

我正在使用下面的代码将 JSON 数据填充到网格视图中,但我得到的是空页面,页面上没有填充记录

 public void Page_Load(object sender, EventArgs e)
{
  DataTable dt = new DataTable();
  dt.Columns.AddRange(new DataColumn[5] 
  {
    new DataColumn("Order Number", typeof(string)),
    new DataColumn("Order Date", typeof(string)),
    new DataColumn("Brand Name", typeof(string)),
    new DataColumn("Product Name", typeof(string)),
    new DataColumn("Quantity", typeof(string))
  });
  string url = new System.Net.WebClient().DownloadString(urlname);
  JArray jsonArray = JArray.Parse(url);
  for (int i = 0; i < jsonArray.Count; i++)
  {
   CustomerOrder isiData = (new 
     JavaScriptSerializer()).Deserialize<CustomerOrder>(jsonArray[i].ToString());
    for (int j = 0; j < isiData.products.Count; j++)
    {
       DataRow dr = dt.NewRow();
       dr["Order Number"] = isiData.orderNumber;
       dr["Order Date"] = isiData.orderDate;
       dr["Brand Name"] = isiData.products[4].BrandName;
       dr["Product Name"] = isiData.products[3].ProductName;
       dr["Quantity"] = isiData.products[2].quantity;
       dt.Rows.Add(dr);
     }
   }
   if (dt.Rows.Count > 0)
   {
    //Bind DataTable to your GridView
    GridView1.DataSource = dt;
    GridView1.DataBind();
   }
}
public class CustomerOrder
{
 public string orderNumber { get; set; }
 public string orderDate { get; set; }
 public string totalAmount { get; set; }
 public string status { get; set; }
 public string quantity { get; set; }
 public List<Product> products { get; set; }
}
public class Product
{
 public string BrandName { get; set; }
 public string ProductName { get; set; }
 public string quantity { get; set; }
}

当我反序列化 JSON 数据时,我从 URL 获取 JSON 数据列表,但我想将该数据填充到 gridview 中。请帮助我谢谢

标签: c#asp.netjsonaspxgridview

解决方案


推荐阅读