Hi @Aaron soggi ,
Based on the API response data, you could try to create the following models:
public class City
{
[JsonProperty]
public string country { get; set; }
[JsonProperty]
public string city { get; set; }
[JsonProperty]
public int count { get; set; }
[JsonProperty]
public int locations { get; set; }
[JsonProperty]
public DateTime firstUpdated { get; set; }
[JsonProperty]
public DateTime lastUpdated { get; set; }
[JsonProperty]
public string[] parameters { get; set; }
}
public class Meta
{
public string name { get; set; }
public string license { get; set; }
public string website { get; set; }
public int page { get; set; }
public int limit { get; set; }
public int found { get; set; }
}
public class RootModel
{
public Meta meta { get; set; }
public List<City> results { get; set; }
}
Then, use the following code to convert the response data and get the City:
IEnumerable<City> city = null;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://docs.openaq.org/");
var responseTask = client.GetAsync("v2/cities");
responseTask.Wait();
var result = responseTask.Result;
if (result.IsSuccessStatusCode)
{
var readData = result.Content.ReadAsStringAsync();
readData.Wait();
//using System.Text.Json;
var jsonresult = JsonSerializer.Deserialize<RootModel>(readData.Result);
city = jsonresult.results;
}
else
{
city = Enumerable.Empty<City>();
}
}
The result like this:
Update:
We can also use the following code:
public async Task<IActionResult> IndexAsync(string city)
{
var client = new HttpClient();
client.BaseAddress = new Uri("https://docs.openaq.org/");
var results = await client.GetFromJsonAsync<RootModel>($"v2/cities").ConfigureAwait(false);
return View();
}
The result like this:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion