❔ Why is my deserialize not working here:
private static void SearchGame() { Write("Title: "); string title = ReadLine(); HttpResponseMessage response = null; response = httpClient.GetAsync($"games/{title}").Result; response.EnsureSuccessStatusCode(); var content = response.Content.ReadAsStringAsync().Result; var games = JsonSerializer.Deserialize<List<GameDTO>>(content); foreach (var game in games) { WriteLine($"Id: {game.Id}"); WriteLine($"Title: {game.Title}"); WriteLine($"Description: {game.Description}"); WriteLine($"Release Date: {game.ReleaseDate}"); WriteLine($"Genre: {game.Genre}"); WriteLine($"Image URL: {game.ImageUrl}"); WriteLine(); } ReadKey(); Clear(); }
12 Replies
$details
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
Include what error(s) you are getting, as well as the code without bad formatting
And what result you are expecting
And show the json in
content
im doing a Get request to this
static readonly HttpClient httpClient = new HttpClient()
{
BaseAddress = new Uri("http://localhost:5000/api/")
};
and it workes but the problem is that all the values is shown as null here
WriteLine($"Id: {game.Id}");
WriteLine($"Title: {game.Title}"); WriteLine($"Description: {game.Description}"); WriteLine($"Release Date: {game.ReleaseDate}"); WriteLine($"Genre: {game.Genre}"); WriteLine($"Image URL: {game.ImageUrl}");the result should be, when I search for the game it should show all the info like title, description, release date, genre and image url.
it's supposed to show this
but instead shows this
STJ is case sensitive, u either need to use Attribute JsonPropertyName on each property to specify which json property to take the value from or pass some json options argument with case insensitive on
example https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/character-casing
How to enable case-insensitive property name matching with System.T...
Learn how to enable case-insensitive property name matching while serializing to and deserializing from JSON in .NET.
Tyvm, you're the best feel dumb now, don't know how I missed that. Done it on all the other methods except this.🤦♂️
Or you can tell STJ to not be case-sensitive
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.