Henry
Henry

Reputation: 317

Cannot Convert From String to NewtonSoft.Json.JsonReader

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,

Upvotes: 17

Views: 34110

Answers (2)

luisantruiz
luisantruiz

Reputation: 241

Try

using System.Text.Json;

instead of

using Newtonsoft.Json;

Upvotes: 19

sahel
sahel

Reputation: 311

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

Upvotes: 31

Related Questions