How to convert JSON data to DataTable in ASP.Net

Last Reply on Sep 23, 2016 07:25 AM By dharmendr

Posted on Sep 23, 2016 07:20 AM
Here's a sample code and a json data in the link 
 Please take a look at the json data in the above link
I have tried this
var postBody = "screen_name=" + Uri.EscapeDataString(screen_name);//
resource_url += "?" + postBody;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
request.Headers.Add("Authorization", authHeader);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
var response = (HttpWebResponse)request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
var objText = reader.ReadToEnd();
myDiv.InnerHtml = objText;/*s*/
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("id_str", typeof(string));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("screen_name", typeof(string));
dt.Columns.Add("location", typeof(string));
dt.Columns.Add("description", typeof(string));
dt.Columns.Add("url", typeof(string));
dt.Columns.Add("followers_count", typeof(string));
dt.Columns.Add("friends_count", typeof(string));
dt.Columns.Add("listed_count", typeof(string));
dt.Columns.Add("created_at", typeof(string));
dt.Columns.Add("favourites_count", typeof(string));

JArray jsonDat = JArray.Parse(objText);
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonDat)))
foreach (var datas in jsonDat.users)
dt.Rows.Add(, datas.users.id_str,, datas.users.screen_name, datas.users.location, datas.users.description, datas.users.url, datas.users.followers_count, datas.users.friends_count, datas.users.listed_count, datas.users.created_at, datas.users.favourites_count);


 Also i have added a class

public class users
public string Id { get; set; }
public string id_str { get; set; }
public string name { get; set; }
public string screen_name { get; set; }
public string location { get; set; }
public string description { get; set; }
public string url { get; set; }
public string followers_count { get; set; }
public string friends_count { get; set; }
public string listed_count { get; set; }
public string created_at { get; set; }
public string favourites_count { get; set; }

Any help would be great.

Posted on Sep 23, 2016 07:25 AM