The JSON value could not be converted to System.Collections.Generic.List`

07735608 20 Reputation points
2023-04-11T00:57:47.4966667+00:00

Hi, My JSON is not converting to a list. JsonException: The JSON value could not be converted to System.Collections.Generic.List`1[Amiibo.Models.NintendoAmiibos]. Path: $ | LineNumber: 0 | BytePositionInLine: 1. My code:

using var client = new HttpClient();
var result = await client.GetAsync("https://www.amiiboapi.com/api/amiibo/");
if (result.IsSuccessStatusCode)
{    var json = await result.Content.ReadAsStringAsync();    
     return JsonSerializer.Deserialize<List<NintendoAmiibos>>(json);
}

return new List<NintendoAmiibos>();

The class:

public class Release
{    
     [JsonPropertyName("au")] public string Au { get; set; }    
     [JsonPropertyName("eu")] public string Eu { get; set; }    
     [JsonPropertyName("jp")] public string Jp { get; set; }    
     [JsonPropertyName("na")] public string Na { get; set; }
}

public class NintendoAmiibos
{    
     [JsonPropertyName("amiiboSeries")] public string AmiiboSeries { get; set; }    
     [JsonPropertyName("character")] public string Character { get; set; }    
     [JsonPropertyName("gameSeries")] public string GameSeries { get; set; }    
     [JsonPropertyName("head")] public string Head { get; set; }    
     [JsonPropertyName("image")] public string Image { get; set; }    
     [JsonPropertyName("name")] public string Name { get; set; }    
     [JsonPropertyName("release")] public Release Release { get; set; }    
     [JsonPropertyName("tail")] public string Tail { get; set; }    
     [JsonPropertyName("type")] public string Type { get; set; }
}

Thank you in advance!

Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2023-04-11T02:04:18.2433333+00:00

    The result is not a list it is a single object that contains a list. .NET Core Console application

    using HttpClientConsole;
    using System.Text.Json;
    
    HttpClient client = new HttpClient();
    Rootobject root = new Rootobject();
    
    var result = await client.GetAsync("https://www.amiiboapi.com/api/amiibo/");
    if (result.IsSuccessStatusCode)
    {
        var json = await result.Content.ReadAsStringAsync();
        root = JsonSerializer.Deserialize<Rootobject>(json);
    }
    
    foreach (var item in root.amiibo)
    {
        Console.WriteLine(item.character);
    }
    
        public class Rootobject
        {
            public Amiibo[] amiibo { get; set; }
        }
    
        public class Amiibo
        {
            public string amiiboSeries { get; set; }
            public string character { get; set; }
            public string gameSeries { get; set; }
            public string head { get; set; }
            public string image { get; set; }
            public string name { get; set; }
            public Release release { get; set; }
            public string tail { get; set; }
            public string type { get; set; }
        }
    
        public class Release
        {
            public string au { get; set; }
            public string eu { get; set; }
            public string jp { get; set; }
            public string na { get; set; }
        }
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.