C
C#17mo ago
palusi

❔ problem loading json into dynamic object

I have a problem loading a json file into a dynamic object in c#. My problem is that some parameters of the c# file have a "-" in the keyword, such as the "official-artwork" parameter. Under what name can I access it now? My Code:
string json = "https://pokeapi.co/api/v2/pokemon/pikachu";
dynamic data = JsonConvert.DeserializeObject<dynamic>(json);
Console.WriteLine(data.sprites.other.official_artwork.front_default);
string json = "https://pokeapi.co/api/v2/pokemon/pikachu";
dynamic data = JsonConvert.DeserializeObject<dynamic>(json);
Console.WriteLine(data.sprites.other.official_artwork.front_default);
8 Replies
ero
ero17mo ago
You shouldn't be using dynamic And it's recommended not to use NewtonSoft There are Json to c# generators online, and visual studio allows you to paste Json as c# classes as well
Thinker
Thinker17mo ago
To answer your actual question, afaik both Newtonsoft and System.Text.Json (the newer generally better library) allows you to specify the JSON names for each property in a class. Which is also why you shouldn't be using dynamic
palusi
palusiOP17mo ago
whats wrong with newtonsoft? And how exactly can i paste json as c# classes in vs?
Thinker
Thinker17mo ago
Newtonsoft is just kinda... outdated? System.Text.Json is built-in, faster, and can do everything that Newtonsoft can. Afaik it's a button under "edit" which says "paste special"
anita
anita17mo ago
Not actually everything, but most of it.
Thinker
Thinker17mo ago
Yeah although I think it covers most the features most people need
palusi
palusiOP17mo ago
okay thank you all
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server