I want to put nested JSON array in gridview in asp.net. I am adding my nested JSON array code.
[
{
"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
}
]
I am using below code for populate JSON data into grid view but I am getting Empty page no records are populated on page
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; }
}
When I deserialize the JSON data I am getting List of JSON data from URL but I want to populate that data into gridview. Please help me Thanks