C
C#12mo 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
ero12mo 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
Thinker12mo 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
palusi12mo ago
whats wrong with newtonsoft? And how exactly can i paste json as c# classes in vs?
Thinker
Thinker12mo 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"
chef drone builder
Not actually everything, but most of it.
Thinker
Thinker12mo ago
Yeah although I think it covers most the features most people need
palusi
palusi12mo ago
okay thank you all
Accord
Accord12mo 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
More Posts
❔ ConfigureAwait(false) (IntelliCode Suggestion)Can someone help me to understand why IntelliCode is suggesting ConfigureAwait(false) at line 16 `aw✅ Class as a field?Suppose I have Class1, Class2, and Class3. Class2 is just a container of information. At runtime I w❔ Lazily evaluating Dictionary in update loop with good performance## Situation I have an update loop which receives some data which is Dictionary-like. Due to a versi❔ Hello everyone, I'm creating an application with ASP.NET Core,Hello everyone, I'm creating an application with ASP.NET Core, I used to insert images by typing the❔ App resource files conventionWhen your app depends on files you read from disk, what's the convention for where to store them? Fo❔ NSubstitute on IEnumerableHey - I'm trying to "freeze" an `IEnumerable<>` and return a mocked enumerator However, when doinmg❔ how do i include a range of numbers into a reply like 1-1000 for my thingstring correct = "well done. "; string wrong = "nope.try again. "; Console.WriteLine("Hello."); ❔ Xamarin.Forms Android App problem using Application.Context.StartActivity(intent)Hi, in short, I created a super small Xamarin.Forms Android App. All I need is just to press a butto❔ How can I test a custom IValueResolver in AutoMapper 12?With the latest release of AutoMapper,the public constructor in the ResolutionContext class has been✅ hey am a student and i need to pass exams without learning anybody helphey am a student and i need to pass exams without learning anybody help