'C# retrieve data from Bored API
I'm trying to retrieve data from https://www.boredapi.com/api/activity, a public API, with the following code in C#:
namespace TesteAval
{
public partial class Form1 : Form
{
private List<Activity> ActList;
private Activity Act;
public Form1()
{
InitializeComponent();
LoadActivities();
}
private async void LoadActivities()
{
var client = new HttpClient();
client.BaseAddress = new Uri("https://www.boredapi.com/");
var response = await client.GetAsync("/api/activity");
//var response = await client.GetAsync("/api/activity?key=5881028");
var result = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
MessageBox.Show(response.ReasonPhrase);
return;
}
ActList = JsonConvert.DeserializeObject<List<Activity>>(result);
Act = JsonConvert.DeserializeObject<Activity>(result);
listBoxDashBoard.DataSource = ActList;
listBoxDashBoard.DisplayMember = "TypeActivitySimple";
}
}
}
Already made the conversion via postman (retrieved the json) and converted to C" (via json2c#). My goal is to create a List ActList with 10 objects but get an error. Also tried to retrieve just one object to var Act but got another error. I tested this exact same code (with different class from json2c#) with another API http://cambios.somee.com/api/Rates and it works.
Also already have the class (from json2C# converter):
namespace TesteAval
{
public class Activity
{
public string activity { get; set; }
public string type { get; set; }
public int participants { get; set; }
public double price { get; set; }
public string link { get; set; }
public string key { get; set; }
public double accessibility { get; set; }
public string TypeActivitySimple
{
get
{
return $"{activity} - {type} ";
}
}
public string TypeActivity
{
get
{
return $"{activity} - {type} - {participants} - {price} - {link} - {key} - {accessibility}";
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
