c# - 如何在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 中。请帮助我谢谢
解决方案
推荐阅读
- rust - 哪一个在 rust 中更惯用?
- orocrm - ORO 从 3.1.19 迁移到 4.1.2
- julia - 在函数名称之前或之后矢量化点?
- angular6 - 如何在 angular google chart-angular 2+ 中获取数据事件点击
- ios - 尝试将 maximumValue 设置为 UISlider 时应用程序崩溃
- javascript - 无法通过 mockData 搜索
- d3.js - 无法重置焦点顺序条形图
- javascript - React 组件道具不会随 redux 商店更新
- javascript - 使用 React Native PanGestureHandler 从下到上移动框
- arrays - 使用矢量化方法而不是 FOR 循环创建多维移位数组