<h1>Load JSon in GridView</h1>
<div>
<asp:GridView ID="GridView1" runat="server" Width="974px" CellPadding="4" ForeColor="#333333" GridLines="None" EmptyDataText="Data Not Found">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
</div>
Code Behind:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DeserializeNestedJSonInASP.NET
{
public partial class BindJsonInGridView : System.Web.UI.Page
{
string _URL = "https://api.icarol.com/v1/Resource/Taxonomy?db=2285";
protected void Page_Load(object sender, EventArgs e)
{
WebRequest req = WebRequest.Create(_URL);
req.Credentials = CredentialCache.DefaultCredentials;
req.Method = "GET";
req.Headers.Add(ConfigurationManager.AppSettings["key"].ToString());
req.ContentType = "application/json; charset=utf-8";
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
StreamReader re = new StreamReader(stream);
String json = re.ReadToEnd();
json = "{\"SalesPerson\":" + json + "}";
wrapper w = (wrapper)new JavaScriptSerializer().Deserialize(json, typeof(wrapper));
if (w.salesperson.Count > 0)
{
GridView1.DataSource = w.salesperson;
GridView1.DataBind();
}
}
}
}
Output: