Cannot Convert From String to NewtonSoft.Json.JsonReader

Viewed 8366

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" in the line var Items = JsonSerializer.Deserialize<Dictionary<string, Paises>>(content);

Here is the entire routine:

public static async Task<List<Paises>> GetPaisesActivosAsync()
{
    string baseUri = new BaseUri().baseUri;
    string sufixUri = "/PaisesApi/GetActives";
    var uri = baseUri + sufixUri;

    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,

2 Answers

Use JsonConvert.DeserializeObject() instead. I was having the same issue on C#. The error should disappear after using JsonConvert instead of JsonSerializer.

Try

using System.Text.Json;

instead of

using Newtonsoft.Json;
Related