Newtonsoft .NET Deserialization
Hey, I would like to deserialize JSON string but I'm actually out of ideas. I have tried JToken to do it by path but it was failure for me. It's complicated for me because in the items_list objects starts with item name and I just can not make class to deserialize the json as far as I know. I suppose that there is easy way to do it but I do not have that much knowledge in JSON. Here's my JSON example. Appreciate any ideas/help.
21 Replies
I have also tried some JSON to C# Generators but failed.
have you tried like JsonConvert.DeserializeObject(jsonfile) ?
and store it into dynamic variable so you can test to see if it deserialize, if it work after that create strongly typed model to deserialize and you done
What's your project? .net core, .net framework, or just .net?
The reason I ask is because
System.Text.Json
exists and is what should be used instead of Newtonsoft
first order of business is making sure your models match the json.
There exists some plugin for Visual Studio that can autogenerate C# class based on json, or you can use some external website for it (json2csharp).
As soon as you've generated those files, you should be able to do what @neSHa said if you'd rather keep using newtonsoft, or you can use System.Text.Json.JsonSerializer.Deserialize<MyClass>(json);
to achieve the same result (where MyClass is the root level class of your json structure).
reason I recommend newtonsoft is because it is not an external packageisnt JsonSerializer part of .net framework, and Newtonsoft.Json third-party library? or am i worng
Newtonsoft.Json have a lot more features and options compared to JsonSerializer. but if he doesnt need anything specific, its better to use JsonSerializer for simplify
newtonsoft is third party and introduces a need for frequent security patching and is in that way friction you don't need.
There's very little in newtonsoft that isn't also in STJ
.NET Core
well didnt know that, i through there is lot of difference between these two, where is Newtonsoft.Json bigger, will check them out in details for sure now
I am speaking for the commonplace usage 🙂 There may be more features for advanced use, but for simple serializing/deserializing with some jsonserializer options then stj is fine
The auto generators are making class for every element in items_list, I have in the list more than 2000 elements and I can not imagine 2000 classes in my code like this
That is very weird. What did you use? json2csharp.com or something else?
json2csharp.com
why cant you use dynamic to check whenever is it deserializing in good way first?
what you mean by use dynamic, I did and its working if you meant this
So most class generators try to trim elements down to a common shared class, and you will get multiple instances of these. If your dataset is sane, then you should have nowhere near 2000 classes. But maybe a two-digit amount of classes, sure.
I was working with the spotify api once, and when deserializing the json for a playlist I ended up with 25 classes I think. That's not uncommon.
There could also be a problem with your dataset
for example:
This will be deserialized into a class that has two properties, one named "ABookAboutWar" for example. In a more sane dataset you would instead have this stored as elements in an array.
That's how the data should look to avoid creating as many classes as the items_list contains elements right?
No, not quite. You're not using an array here
in JSON an array is
[]
This would make the contents inside items_list deserialize into an array
instead of one class per element
Do you understand the difference between {}
and []
in json?Yes
Then.. in your json, the one you posted above - your "items_list" isn't actually a list.
It's a class with an anonymous object which has many properties like name, marketplace, tradeable etc
I know but tried to look for a way to deserialize it without making a lot of classes, thats not my created json I'm just working with it but gonna drop it and make a own one
Something like this would work as a list
MyElements
would get serialized into List<Element>
where Element is just something that contains the three properties seen hereThanks for yours help Fyren and neSHa gonna close the thread.