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});
}
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;
}
}
}
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.
26 Replies
MODiX
MODiX3y ago
Ero#1111
REPL Result: Success
using Newtonsoft.Json;

var json = """
{
"Name": "Willy",
"Time": 316
}
""";

var hs = JsonConvert.DeserializeObject<Highscore>(json, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});

Console.WriteLine(hs);

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;
}
}
using Newtonsoft.Json;

var json = """
{
"Name": "Willy",
"Time": 316
}
""";

var hs = JsonConvert.DeserializeObject<Highscore>(json, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});

Console.WriteLine(hs);

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;
}
}
Console Output
Willy - 316
Willy - 316
Compile: 692.423ms | Execution: 158.707ms | React with ❌ to remove this embed.
ero
ero3y ago
¯\_(ツ)_/¯
𝔉𝔩𝔢𝔢𝔠𝔢
Thanks xD What did you change exactly?
ero
ero3y ago
Nothing Just showing your that it should work
𝔉𝔩𝔢𝔢𝔠𝔢
Bruh 😓 thanks anyways :)
MODiX
MODiX3y ago
Check your private messages, @𝕭𝖔𝖙𝖙𝖑𝖊_𝕭𝖆𝖗𝖔𝖓.
MODiX
MODiX3y ago
Ero#1111
REPL Result: Success
using Newtonsoft.Json;

var json = """
[
{
"Name": "Willy",
"Time": 316
},
{
"Name": "Willy",
"Time": 316
}
]
""";

var hs = JsonConvert.DeserializeObject<List<Highscore>>(json, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});

Console.WriteLine(hs[0]);

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;
}
}
using Newtonsoft.Json;

var json = """
[
{
"Name": "Willy",
"Time": 316
},
{
"Name": "Willy",
"Time": 316
}
]
""";

var hs = JsonConvert.DeserializeObject<List<Highscore>>(json, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});

Console.WriteLine(hs[0]);

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;
}
}
Console Output
Willy - 316
Willy - 316
Compile: 742.622ms | Execution: 179.356ms | React with ❌ to remove this embed.
ero
ero3y ago
Still works for a list too
𝔉𝔩𝔢𝔢𝔠𝔢
Hm Might have found it Seems the json string has been altered somehow
["{\"Name\":\"Pet\",\"Time\":316}","{\"Name\":\"Pet\",\"Time\":278}"]
["{\"Name\":\"Pet\",\"Time\":316}","{\"Name\":\"Pet\",\"Time\":278}"]
ero
ero3y ago
where are you getting this input?
𝔉𝔩𝔢𝔢𝔠𝔢
Im sending the data from my game to a web api then using the get method to retrieve it from the site
ero
ero3y ago
is the web api strictly a middleman for this or does it do more?
𝔉𝔩𝔢𝔢𝔠𝔢
No it is just supposed to store data
ero
ero3y ago
then why not just send the data from the game directly to the app?
𝔉𝔩𝔢𝔢𝔠𝔢
That is what i am attempting using the post and get endpoints
ero
ero3y ago
well no, that's a middleman
𝔉𝔩𝔢𝔢𝔠𝔢
this is what the repo looks like roughly
𝔉𝔩𝔢𝔢𝔠𝔢
The WebApi parts job is to take the data sent from the reactiongame and store it into a SQLite database The get method should then get that data and return it to the game
ero
ero3y ago
right well, anyway. that seems like incorrectly formatted json there shouldn't be quotes after [ and before ]
𝔉𝔩𝔢𝔢𝔠𝔢
ait then i will look into this :)
Pobiega
Pobiega3y ago
that looks like a List<string> where each string is a json serialized object
ero
ero3y ago
yup also consider using System.Text.Json instead of Newtonsoft
𝔉𝔩𝔢𝔢𝔠𝔢
Yeah I'll try that Thanks ^^
ero
ero3y ago
it won't fix this! but it is still just. better in every way
MODiX
MODiX3y ago
Ero#1111
REPL Result: Success
using System.Text.Json;

var json = """
[
{
"Name": "Pet",
"Time": 316
},
{
"Name": "Pet",
"Time": 278
}
]
""";

var hs = JsonSerializer.Deserialize<List<Highscore>>(json);
Console.WriteLine(hs[0]);

record Highscore(
string Name,
int Time);
using System.Text.Json;

var json = """
[
{
"Name": "Pet",
"Time": 316
},
{
"Name": "Pet",
"Time": 278
}
]
""";

var hs = JsonSerializer.Deserialize<List<Highscore>>(json);
Console.WriteLine(hs[0]);

record Highscore(
string Name,
int Time);
Console Output
Highscore { Name = Pet, Time = 316 }
Highscore { Name = Pet, Time = 316 }
Compile: 753.600ms | Execution: 95.491ms | React with ❌ to remove this embed.

Did you find this page helpful?