I am developing a new platform which consumes data from an API of the SBIF, but the JSON with the data that it returns to me comes like this
{
"Dolares":
[
{
"Valor": "603,31",
"Fecha": "2018-04-27"
{
]
}
To use it httpClient use and throw me a "deserialize" error I have several techniques that I found in here but none of them works for me. The error that returns the view is this.
Can not deserialize the current JSON object (eg {"name": "value"}) into type 'System.Collections.Generic.List'1 [DetamaticWeb.Models.DolarViewModel]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from to JSON object. Path 'Dolares', line 2, position 12.
Would someone be so kind as to tell me how to use this api please?
Controller
string Baseurl = "http://api.sbif.cl/";
public async Task<ActionResult> Index()
{
List<DolarViewModel> EmpInfo = new List<DolarViewModel>();
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Sending request to find web api REST service resource GetAllEmployees using HttpClient
HttpResponseMessage Res = await client.GetAsync("api");
//Checking the response is successful or not which is sent using HttpClient
if (Res.IsSuccessStatusCode)
{
//Storing the response details recieved from web api
var EmpResponse = Res.Content.ReadAsStringAsync().Result;
//Deserializing the response recieved from web api and storing into the Employee list
EmpInfo = JsonConvert.DeserializeObject<List<DolarViewModel>>(EmpResponse);
}
//returning the employee list to view
return View(EmpInfo);
}
}
Model
public class DolarViewModel
{
public string Valor { get; set; }
public string Fecha { get; set; }
}
Thank you.