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); 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; }
};
22 Replies
ero
ero3y ago
name is not a Dictionary<string, int>
Cvfe Cvt ᵖᵘʳʳ
Thank you, what data structure is it?
ero
ero3y ago
what pairs are in the name element?
Cvfe Cvt ᵖᵘʳʳ
I am unsure of what you mean. I'm not familiar with C# dictionaries, only python. It is my understanding that the language is the key and the pokemon in the value in the name element.
pip
pip3y ago
what you should do to test this is make a dictionary, then serialize it. see what comes out. i'm thinking that you may have to explicitly specify {"key" : "english", "value":"Bulbasaur"} but that's just a guess
ero
ero3y ago
no even python has types what type is "english"
Cvfe Cvt ᵖᵘʳʳ
Oh, I suppose a string.
ero
ero3y ago
and what type is "Bulbasaur"
Cvfe Cvt ᵖᵘʳʳ
A string as well.
ero
ero3y ago
so what should be the key and value types in the name Dictionary?
Cvfe Cvt ᵖᵘʳʳ
Oh! I didn't realize that was still set to int. I had changed everything to string earlier to test.
ero
ero3y ago
there you go
Cvfe Cvt ᵖᵘʳʳ
It still doesn't run though lol.
ero
ero3y ago
any errors? oh, jsonconvert 🤢
Cvfe Cvt ᵖᵘʳʳ
I apologize for the image. VS wasn't showing an error. Had to print screen the console. I didn't realize the error until now
ero
ero3y ago
can you show your full code? but if possible, just don't use newtonsoft
MODiX
MODiX3y ago
Ero#1111
REPL Result: Success
var json = """
{
"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
}
}
""";

var pokemon = System.Text.Json.JsonSerializer.Deserialize<Pokemon>(json);
return pokemon;

class Pokemon
{
public int id { get; set; }
public Dictionary<string, string> name { get; set; }
public string[] type { get; set; }
public Dictionary<string, int> stats { get; set; }
}
var json = """
{
"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
}
}
""";

var pokemon = System.Text.Json.JsonSerializer.Deserialize<Pokemon>(json);
return pokemon;

class Pokemon
{
public int id { get; set; }
public Dictionary<string, string> name { get; set; }
public string[] type { get; set; }
public Dictionary<string, int> stats { get; set; }
}
Result: Pokemon
{
"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
}
}
{
"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
}
}
Compile: 557.944ms | Execution: 54.307ms | React with ❌ to remove this embed.
Cvfe Cvt ᵖᵘʳʳ
The entire code is at the top. I'll try System.Text.Json.JsonSerializer.Deserialize<Pokemon>(json); Newton was what I found online. How did you color your blocks?
ero
ero3y ago
$code
MODiX
MODiX3y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
ero
ero3y ago
$codegif
Want results from more Discord servers?
Add your server