Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to deserialise and the JSON value could not be converted to class(?)

very newbie programmer here trying to learn C#. I’m trying to deserialise a JSON file and then print the values inside. However, I’m met with a Jsonexception in the form of cannot be converted to the class I made.

The code in question:

public class Games
{
    public string? Title { get; set;}
    public int ReleaseYear { get; set;}
    public double Rating { get; set;}
}

public class ReadAndParseJSONFromFile()
{
    public static void ReadingJSON()
    {
        string filePath = @"C:\Users\Kodain\C#\Game Data Parser\games.json";
        string jsonString = File.ReadAllText(filePath);
        Games games = JsonSerializer.Deserialize<Games>(jsonString)!;
        System.Console.WriteLine("Loaded games are: ");
        System.Console.WriteLine($"{games.Title}, released in {games.ReleaseYear}, rating: {games.Rating}");
    }
}

and the provided JSON file:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  {
    "Title": "Stardew Valley",
    "ReleaseYear": 2016,
    "Rating": 4.9
  },
  {
    "Title": "Frostpunk",
    "ReleaseYear": 2017,
    "Rating": 4.7
  },
  {
    "Title": "Oxygen Not Included",
    "ReleaseYear": 2017,
    "Rating": 4.8
  },
  {
    "Title": "Red Dead Redemtpion II",
    "ReleaseYear": 2018,
    "Rating": 4.8
  },
  {
    "Title": "Portal 2",
    "ReleaseYear": 2011,
    "Rating": 4.8
  }
]

I tried to use the Microsoft documentation on how to deserialise since I’ve never done it before with JSON, only text files. I expected the program to read the JSON file, then print out all the games with title, release year, and rating. Instead I got a Jsonexception that tells me that the JSON value cannot be converted to Games:

Exception has occurred: CLR/System.Text.Json.JsonException
An unhandled exception of type ‘System.Text.Json.JsonException’ occurred in System.Text.Json.dll: ‘The JSON value could not be converted to Games.’

I tried my google-fu, but I too much of a noob and cannot seem to find a solution to my problem. I am quite stumped.

>Solution :

The JSON is having a List of games. You should Deserialize to List<Games>

     public static void ReadingJSON()
        {
            string filePath = @"C:\Users\Kodain\C#\Game Data Parser\games.json";
            string jsonString = File.ReadAllText(filePath);
            var games = JsonSerializer.Deserialize<List<Games>>(jsonString)!;
            System.Console.WriteLine("Loaded games are: ");
            foreach(var game in games)
            {
                 System.Console.WriteLine($"{game.Title}, released in 
                  {game.ReleaseYear}, rating: {game.Rating}");
            }
        }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading