Reputation: 161
I am new to api stuff. I want to get the result of this api(http://services.groupkt.com/country/get/all) in c# code. can you help me by suggesting any code and tutorial as well. thanks i have tried this code but it doesnot work.
public async Task DownloadData()
{
string url = string.Format("http://services.groupkt.com/country/get/all");
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json");
var jsonString = await client.GetStringAsync(url);
JToken token = JToken.Parse(jsonString);
foreach (var item in token)
{
txtarea.Value= item.ToString();
}
}
Upvotes: 0
Views: 15113
Reputation: 1796
First of all use
client.GetStringAsync(url).Result
instead of
client.GetStringAsync(url)
Second after you received the json, it becomes very simple to parse the result. I saw the previous answeres and they were all using a loop, which is not a good idea in my opinion to parse.
Use Newtonsoft.Json
library and its very handy in such situations. I've parsed your json response using this library.
Make a class of result, i.e.
public class result
{
public string name { get; set; }
public string alpha3_code { get; set; }
public string alpha2_code { get; set; }
}
put this code after getting json response for parsing your json.
JObject jsonResponse = JObject.Parse(jsonString);
JObject objResponse = (JObject)jsonResponse["RestResponse"];
Dictionary<string, JArray> _Data = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, JArray>>(objResponse.ToString());
var results = _Data["result"].ToObject<List<result>>();
It works perfectly, I've tested this.
Don't forget to add Newtonsoft.Json AND Newtonsoft.Json.Linq namespaces
Upvotes: 3
Reputation: 3412
Just to add, I would prefer dynamics here so the code communicates more clearly.
public async Task DownloadData()
{
string url = $"http://services.groupkt.com/country/get/all";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json");
string response = await client.GetStringAsync(url);
dynamic json = JToken.Parse(response);
foreach (var item in token.RestResponse.result)
{
//Note: Your over writing the text here for each item you pass
// Did you mean to concat instead? += "\n\r" + item.name;
txtarea.InnerText = item.ToString();
}
}
Now as for just doing txtarea.InnerText = ...
in a question you labeled with "ASP.NET-Web-Api" seems a bit odd, is it an ASP.NET MVC with Web Api or a more classic Web Forms application?
Upvotes: 0
Reputation: 482
Your code fetches the response correctly. But parsing incorrectly.
Try the below full parsing code.
public async Task DownloadData()
{
string url = string.Format("http://services.groupkt.com/country/get/all");
string top_parent_key_name = "RestResponse";
string second_parent_key_name = "result";
string field_one_key_name = "name";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json");
var jsonString = await client.GetStringAsync(url);
JToken token = JToken.Parse(jsonString);
foreach (var item in token[top_parent_key_name][second_parent_key_name])
{
txtarea.InnerText = item[field_one_key_name].ToString();
}
}
Upvotes: 0