TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Vikash Kumar
NA
959
21.1k
How to convert a JSON data from web API to a DataTable?
Sep 23 2016 9:11 AM
Hi,
Here's a sample code and a json data
https://dev.twitter.com/rest/reference/get/followers/list
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, datas.users.id_str, datas.users.name, 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 created a class named users
public class users
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("id_str")]
public string id_str { get; set; }
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("screen_name")]
public string screen_name { get; set; }
[JsonProperty("location")]
public string location { get; set; }
[JsonProperty("description")]
public string description { get; set; }
[JsonProperty("url")]
public string url { get; set; }
[JsonProperty("followers_count")]
public string followers_count { get; set; }
[JsonProperty("friends_count")]
public string friends_count { get; set; }
[JsonProperty("listed_count")]
public string listed_count { get; set; }
[JsonProperty("created_at")]
public string created_at { get; set; }
[JsonProperty("favourites_count")]
public string favourites_count { get; set; }
}
}
Need help on this
Thanks
Reply
Answers (
17
)
Need suggestions for Freelancing or increase the income
How should I transit from C#.net to SAP?