I want to call an API from my ASP.Net Core app, but the response is not in a great format:
[
{
page: 1,
pages: 1,
per_page: 50,
total: 2,
sourceid: "2",
sourcename: "World Development Indicators",
lastupdated: "2021-10-28"
},
[
{
indicator: {
id: "NY.GDP.PCAP.KD.ZG",
value: "GDP per capita growth (annual %)"
},
country: {
id: "CA",
value: "Canada"
},
countryiso3code: "CAN",
date: "2020",
value: -6.33904652535297,
unit: "",
obs_status: "",
decimal: 1
},
{
indicator: {
id: "NY.GDP.PCAP.KD.ZG",
value: "GDP per capita growth (annual %)"
},
country: {
id: "CA",
value: "Canada"
},
countryiso3code: "CAN",
date: "2019",
value: 0.43021113414872,
unit: "",
obs_status: "",
decimal: 1
}
]
]
Here is the API url: https://api.worldbank.org/v2/country/can/indicator/NY.GDP.PCAP.KD.ZG?format=json&mrv=2
I have typed these classes for the response:
public class WorldBankApiResponseGlobal
{
public int Page { get; set; }
public int Pages { get; set; }
public int Per_Page { get; set; }
public int Total { get; set; }
public string Sourceid { get; set; }
public string Sourcename { get; set; }
}
public class WorldBankApiIdValue
{
public string Id { get; set; }
public string Value { get; set; }
}
public class WorldBankApiResponse
{
public WorldBankApiIdValue Indicator { get; set; }
public WorldBankApiIdValue Country { get; set; }
public int Value { get; set; }
public string Date { get; set; }
public string Countryiso3code { get; set; }
}
But, since the response doesn't have property values for the objects (ie: [{},[]] instead of ["data": {}, "indicators": []], I don't know how to properly deserialize the API call response...
Normally, I would do it as such:
var response = await _client.GetFromJsonAsync<List<WorldBankApiResponseGlobal>>(worldBankApiUrl);
But does anyone know how I would do it with this response type?