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();
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)
{
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);
}
}