2
I need to refactor this method to make it work better.
This method works perfectly when calling on an external API Rest
returns list. However, it gives exception when the return object of the external API Rest returns an element only.
public List<T> ChamarAPIListagem<T>(string chamada)
{
HttpResponseMessage response = client.GetAsync(chamada).Result;
if (response.IsSuccessStatusCode)
{
var dados = response.Content.ReadAsAsync<IEnumerable<T>>().Result;
return dados.ToList();
}
return null;
}
The problem is when the API returns an object only. It gives exception error:
Jsonserializationexception: Cannot deserialize the Current JSON Object (e.g. {"name":"value"}) into type 'System.Collections.Generic.Ienumerable`1[Appspot.Ano]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
Although not very relevant, follows an example of the return in JSON
of API
external;
An example of an API return would be this:
[
{"codigo": "320-1", "name": "Zer", "key": "320"},
{"codigo": "201-1", "name": "201", "key": "201"},
{"codigo": "201-1", "name": "201", "key": "201"},
{"codigo": "201-1", "name": "201", "key": "201"},
]