C#C
C#3y ago
Cleo

Newtonsoft.Json Serialization Exception

Trying to deserialize a List<Highscore> for my project and it throws this exception:

Unhandled exception. Newtonsoft.Json.JsonSerializationException: Error converting value "{"Name":"Willy","Time":316}" to type 'ReactionGame.Highscore'

Code (T is List<Highscore>):
    public virtual async Task<T> Get<T>(string url) where T : class
    {
        using var client = new HttpClient();

        var stringResult = await client.GetStringAsync(url);
        return JsonConvert.DeserializeObject<T>(stringResult, new JsonSerializerSettings{NullValueHandling = NullValueHandling.Ignore});
    }


Model:
namespace ReactionGame
{
    public class Highscore
    {
        [JsonProperty]
        public string Name { get; set; }
        [JsonProperty]
        public long Time { get; set; }

        public override string ToString()
        {
            return Name + "  -  " + Time;
        }

        [JsonConstructor]
        public Highscore(string name, long time)
        {
            Name = name;
            Time = time;
        }
    }
}


Anyone know what might be wrong? I'm at a loss.
Was this page helpful?