Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 713
Henry Avatar asked Nov 30 '25 20:11

Henry


1 Answers

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

like image 164
sahel Avatar answered Dec 03 '25 11:12

sahel