I am new in Xamarin Forms and I am trying to create a method that requests a list of items from an API. However, I am not able to compile the solution due to the error message
Cannot Convert From String to NewtonSoft.Json.JsonReader
on the line
var Items = JsonSerializer.Deserialize<Dictionary<string, Paises>>(content);
Here is the entire method:
public static async Task<List<Paises>> GetPaisesActivosAsync()
{
string baseUri = new BaseUri().baseUri;
string suffixUri = "/PaisesApi/GetActives";
var uri = baseUri + suffixUri;
List<Paises> listaPaisesActivos = null;
HttpResponseMessage response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
var Items = JsonSerializer.Deserialize<Dictionary<string, Paises>>(content);
}
return listaPaisesActivos;
}
Thanks in advance for your support.
Regards,
Use JsonConvert.DeserializeObject() instead. I was having the same issue on C#. The error should disappear after using JsonConvert instead of JsonSerializer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With