Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i populate datatable in c# using the below json structure
{
  "store": "Barnes Rd. C.S.C.",
  "storeID": 1001,
  "items": [
    {
      "item": "1K AirteltiGO Scratch Card",
      "details": [
        {
          "startSerial": "64235900000",
          "endSerial": "64235900000",
          "quantity": 1
        }
      ]
    }
  ]
}


What I have tried:

try
           {
               var client = new HttpClient();
               String jsonstring = null;
               int transno = Convert.ToInt32(lbltransactionnumbertovoid.Text);
               int storeid = Convert.ToInt32(Session["StoreID"]);
               client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
               client.BaseAddress = new Uri(Constants.BaseApiAddress);
               client.DefaultRequestHeaders.Accept.Clear();
               client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
               var response = client.GetAsync("url" + storeid + "/" + transno).Result;
               transdetails.Columns.Clear();
               transdetails.Rows.Clear();
               transdetails.Clear();
               //Gridtransactionitemsprint.DataSource = transdetails;
               //Gridtransactionitemsprint.DataBind();
               if (response.IsSuccessStatusCode)
               {
                   jsonstring = response.Content.ReadAsStringAsync().Result;


                   Root deserializedClass = JsonConvert.DeserializeObject<Root>(response.ToString());




                   transdetails = (DataTable)JsonConvert.DeserializeObject(jsonstring, (typeof(DataTable)));
                   if (transdetails != null && transdetails.Rows.Count > 0)
                   {
                       //DataTable DT = new DataTable();
                       gridtransactionsoldserials.DataSource = transdetails;
                       gridtransactionsoldserials.DataBind();
                       ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "$('#transactionsoldserials').modal('show');", true);
                   }
                   else
                   {
                       gridtransactionsoldserials.DataSource = transdetails;
                       gridtransactionsoldserials.DataBind();
                       ScriptManager.RegisterStartupScript(this, this.GetType(), "HidePopup", "$('#transactionsoldserials').modal('hide')", true);
                   }
               }
               else
               {
                   gridtransactionsoldserials.DataSource = transdetails;
                   gridtransactionsoldserials.DataBind();
                   ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "$('#transactionsoldserials').modal('show');", true);
               }

           }
           catch (Exception ex)
           {
               if (Session["username"] == null)
               {
                   Response.Redirect("Login.aspx");
               }
               else
               {
                   Constants.writelog(Session["username"].ToString() + ":" + Session["StoreId"].ToString() + ":" + ex.Message.ToString() + ":" + "Invoice" + ":" + "Transactionitemserials()" + " " + "method was called");
                   lblmessages.Text = ex.Message.ToString();
                   ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "$('#validations').modal('show');", true);
               }
           }
Posted
Updated 18-Feb-23 5:25am

1 solution

This may help: Converting a List to a DataTable[^]

You'll probably need to tweak it a little to deal with the embedded details class, but that shouldn't be a major problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900