How to deserialize dictionary using Newtonsoft [Answered]
I have a Json file with a bunch of pokemon data. I am having trouble deserializing the information. The console is running and showing no errors, but it exits out immediately. This is not a Console.ReadLine() issue because commenting out the:
makes the console stay. I also think I have an error with how I am storing the data. I am not sure how to access a specific item.
// Json Template/*{"id": 1,"name": {"english": "Bulbasaur","japanese": "フシギダネ","chinese": "妙蛙种子","french": "Bulbizarre"},"type": ["Grass","Poison"],"stats": {"HP": 45,"Attack": 49,"Defense": 49,"Sp. Attack": 65,"Sp. Defense": 65,"Speed": 45}}*///Librariesusing Newtonsoft.Json;//1. Get the file from the path and read it!string path = @"C:\Users\c e r e s\Desktop\Pokemon\pokedex.json";string jsonFile = File.ReadAllText(path);//2. Access Pokemon Class and deserialize:Pokemon pkmn = JsonConvert.DeserializeObject<Pokemon>(jsonFile);//3. Iterate values from the arrayConsole.WriteLine(pkmn.id);Console.ReadLine();//Storage structurepublic class Pokemon { public int id { get; set; } public Dictionary<string, int> name { get; set; } public string[] type { get; set; } public Dictionary<string, int> stats { get; set; }};
// Json Template/*{"id": 1,"name": {"english": "Bulbasaur","japanese": "フシギダネ","chinese": "妙蛙种子","french": "Bulbizarre"},"type": ["Grass","Poison"],"stats": {"HP": 45,"Attack": 49,"Defense": 49,"Sp. Attack": 65,"Sp. Defense": 65,"Speed": 45}}*///Librariesusing Newtonsoft.Json;//1. Get the file from the path and read it!string path = @"C:\Users\c e r e s\Desktop\Pokemon\pokedex.json";string jsonFile = File.ReadAllText(path);//2. Access Pokemon Class and deserialize:Pokemon pkmn = JsonConvert.DeserializeObject<Pokemon>(jsonFile);//3. Iterate values from the arrayConsole.WriteLine(pkmn.id);Console.ReadLine();//Storage structurepublic class Pokemon { public int id { get; set; } public Dictionary<string, int> name { get; set; } public string[] type { get; set; } public Dictionary<string, int> stats { get; set; }};