0

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

kiran girase
  • 97
  • 2
  • 13
  • Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – VDWWD Jul 20 '18 at 18:42
  • 1
    The problem is probably one of these lines `isiData.products[4]`. What would happen if there are less than 5 products... Then the index is out of range. – VDWWD Jul 20 '18 at 18:43
  • Thanks VDWWD for reply, But I want to populate that data into gridview, is it good code or can I modify it? – kiran girase Jul 20 '18 at 19:10
  • Thanks VDWWD , I was changed my code and I am getting my answer by little bit change, `for(int j=0;j – kiran girase Jul 22 '18 at 09:19

0 Answers0