© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
26 replies
Cvfe Cvt ᵖᵘʳʳ

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:
Pokemon pkmn = JsonConvert.DeserializeObject<Pokemon>(jsonFile); 
Pokemon pkmn = JsonConvert.DeserializeObject<Pokemon>(jsonFile); 

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}
}
*/

//Libraries
using 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 array
Console.WriteLine(pkmn.id);
Console.ReadLine();

//Storage structure
public 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}
}
*/

//Libraries
using 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 array
Console.WriteLine(pkmn.id);
Console.ReadLine();

//Storage structure
public 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; }
};
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Newtonsoft.Json deserialize issue [Answered]
C#CC# / help
4y ago
Force Newtonsoft to deserialize into certain formats
C#CC# / help
16mo ago
Best way to deserialize duplicate JSON API responses (Using Newtonsoft.JSON)
C#CC# / help
16mo ago
Deserialize JSON to dynamic object without Newtonsoft.Json
C#CC# / help
4y ago