Cvfe Cvt ᵖᵘʳʳ
Cvfe Cvt ᵖᵘʳʳ
CC#
Created by Cvfe Cvt ᵖᵘʳʳ on 3/26/2023 in #help
Error with creating JSON object.
6 replies
CC#
Created by Cvfe Cvt ᵖᵘʳʳ on 10/7/2022 in #help
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; }
};
27 replies