C
C#•2y ago
Bilnord

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.
{
"success": true,
"currency": "USD",
"timestamp": 1674493571,
"items_list": {
"&#39Blueberries&#39 Buckshot | NSWC SEAL": {
"name": "&#39Blueberries&#39 Buckshot | NSWC SEAL",
"marketable": 1,
"tradable": 1,
"classid": "4114517977",
"icon_url": "----",
"icon_url_large": null,
"type": null,
"rarity": "Exceptional",
"rarity_color": "8847ff",
"price": {
"7_days": {
"average": 3.19,
"median": 3.18,
"sold": "289",
"standard_deviation": "12.46",
"lowest_price": 0.99,
"highest_price": 3.61
},
"30_days": {
"average": 2.84,
"median": 2.8,
"sold": "2835",
"standard_deviation": "11.32",
"lowest_price": 0.99,
"highest_price": 3.61
},
"all_time": {
"average": 1.41,
"median": 2.38,
"sold": "157819",
"standard_deviation": "51.15",
"lowest_price": 0.83,
"highest_price": 3.61
}
},
"first_sale_date": "1607036400"
}
}
}
{
"success": true,
"currency": "USD",
"timestamp": 1674493571,
"items_list": {
"&#39Blueberries&#39 Buckshot | NSWC SEAL": {
"name": "&#39Blueberries&#39 Buckshot | NSWC SEAL",
"marketable": 1,
"tradable": 1,
"classid": "4114517977",
"icon_url": "----",
"icon_url_large": null,
"type": null,
"rarity": "Exceptional",
"rarity_color": "8847ff",
"price": {
"7_days": {
"average": 3.19,
"median": 3.18,
"sold": "289",
"standard_deviation": "12.46",
"lowest_price": 0.99,
"highest_price": 3.61
},
"30_days": {
"average": 2.84,
"median": 2.8,
"sold": "2835",
"standard_deviation": "11.32",
"lowest_price": 0.99,
"highest_price": 3.61
},
"all_time": {
"average": 1.41,
"median": 2.38,
"sold": "157819",
"standard_deviation": "51.15",
"lowest_price": 0.83,
"highest_price": 3.61
}
},
"first_sale_date": "1607036400"
}
}
}
21 Replies
Bilnord
BilnordOP•2y ago
I have also tried some JSON to C# Generators but failed.
neSHa
neSHa•2y ago
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
Esa
Esa•2y ago
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 package
neSHa
neSHa•2y ago
isnt 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
Esa
Esa•2y ago
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
Bilnord
BilnordOP•2y ago
.NET Core
neSHa
neSHa•2y ago
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
Esa
Esa•2y ago
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
Bilnord
BilnordOP•2y ago
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
public class 39Blueberries39BuckshotNSWCSEAL
{
public string name { get; set; }
public int marketable { get; set; }
public int tradable { get; set; }
public string classid { get; set; }
public string icon_url { get; set; }
public object icon_url_large { get; set; }
public object type { get; set; }
public string rarity { get; set; }
public string rarity_color { get; set; }
public Price price { get; set; }
public string first_sale_date { get; set; }
}

public class 39MediumRare39CrasswaterGuerrillaWarfare
{
public string name { get; set; }
public int marketable { get; set; }
public int tradable { get; set; }
public string classid { get; set; }
public string icon_url { get; set; }
public object icon_url_large { get; set; }
public object type { get; set; }
public string rarity { get; set; }
public string rarity_color { get; set; }
public Price price { get; set; }
public string first_sale_date { get; set; }
}

public class 39TheDoctor39RomanovSabre
{
public string name { get; set; }
public int marketable { get; set; }
public int tradable { get; set; }
public string classid { get; set; }
public string icon_url { get; set; }
public object icon_url_large { get; set; }
public object type { get; set; }
public string rarity { get; set; }
public string rarity_color { get; set; }
public Price price { get; set; }
public string first_sale_date { get; set; }
}
public class 39Blueberries39BuckshotNSWCSEAL
{
public string name { get; set; }
public int marketable { get; set; }
public int tradable { get; set; }
public string classid { get; set; }
public string icon_url { get; set; }
public object icon_url_large { get; set; }
public object type { get; set; }
public string rarity { get; set; }
public string rarity_color { get; set; }
public Price price { get; set; }
public string first_sale_date { get; set; }
}

public class 39MediumRare39CrasswaterGuerrillaWarfare
{
public string name { get; set; }
public int marketable { get; set; }
public int tradable { get; set; }
public string classid { get; set; }
public string icon_url { get; set; }
public object icon_url_large { get; set; }
public object type { get; set; }
public string rarity { get; set; }
public string rarity_color { get; set; }
public Price price { get; set; }
public string first_sale_date { get; set; }
}

public class 39TheDoctor39RomanovSabre
{
public string name { get; set; }
public int marketable { get; set; }
public int tradable { get; set; }
public string classid { get; set; }
public string icon_url { get; set; }
public object icon_url_large { get; set; }
public object type { get; set; }
public string rarity { get; set; }
public string rarity_color { get; set; }
public Price price { get; set; }
public string first_sale_date { get; set; }
}
Esa
Esa•2y ago
That is very weird. What did you use? json2csharp.com or something else?
Bilnord
BilnordOP•2y ago
json2csharp.com
neSHa
neSHa•2y ago
why cant you use dynamic to check whenever is it deserializing in good way first?
Bilnord
BilnordOP•2y ago
what you mean by use dynamic, I did
var json = JsonConvert.DeserializeObject(myJsonString);
Console.WriteLine(json.ToString());
var json = JsonConvert.DeserializeObject(myJsonString);
Console.WriteLine(json.ToString());
and its working if you meant this
Esa
Esa•2y ago
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:
{
"ABookAboutWar":{
"someProperty":"someValue"
},
"ABookAboutLove":{
"someProperty":"someValue"
}
}
{
"ABookAboutWar":{
"someProperty":"someValue"
},
"ABookAboutLove":{
"someProperty":"someValue"
}
}
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.
Bilnord
BilnordOP•2y ago
"items_list": {
{
"name": "&#39Blueberries&#39 Buckshot | NSWC SEAL",
"marketable": 1,
"tradable": 1,
"classid": "4114517977",
"icon_url": "----",
"icon_url_large": null,
"type": null,
"rarity": "Exceptional",
"rarity_color": "8847ff",
"price": {
"7_days": {
"average": 3.19,
"median": 3.18,
"sold": "289",
"standard_deviation": "12.46",
"lowest_price": 0.99,
"highest_price": 3.61
},
"30_days": {
"average": 2.84,
"median": 2.8,
"sold": "2835",
"standard_deviation": "11.32",
"lowest_price": 0.99,
"highest_price": 3.61
},
"all_time": {
"average": 1.41,
"median": 2.38,
"sold": "157819",
"standard_deviation": "51.15",
"lowest_price": 0.83,
"highest_price": 3.61
}
},
"first_sale_date": "1607036400"
}
}
"items_list": {
{
"name": "&#39Blueberries&#39 Buckshot | NSWC SEAL",
"marketable": 1,
"tradable": 1,
"classid": "4114517977",
"icon_url": "----",
"icon_url_large": null,
"type": null,
"rarity": "Exceptional",
"rarity_color": "8847ff",
"price": {
"7_days": {
"average": 3.19,
"median": 3.18,
"sold": "289",
"standard_deviation": "12.46",
"lowest_price": 0.99,
"highest_price": 3.61
},
"30_days": {
"average": 2.84,
"median": 2.8,
"sold": "2835",
"standard_deviation": "11.32",
"lowest_price": 0.99,
"highest_price": 3.61
},
"all_time": {
"average": 1.41,
"median": 2.38,
"sold": "157819",
"standard_deviation": "51.15",
"lowest_price": 0.83,
"highest_price": 3.61
}
},
"first_sale_date": "1607036400"
}
}
That's how the data should look to avoid creating as many classes as the items_list contains elements right?
Esa
Esa•2y ago
No, not quite. You're not using an array here in JSON an array is []
{
"items_list": []
}
{
"items_list": []
}
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?
Bilnord
BilnordOP•2y ago
Yes
Esa
Esa•2y ago
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
Bilnord
BilnordOP•2y ago
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
Esa
Esa•2y ago
{
"MyElements": [
{
"Name":"",
"Marketable":"",
"Tradeable":""
},
{
"Name":"",
"Marketable":"",
"Tradeable":""
}
]
}
{
"MyElements": [
{
"Name":"",
"Marketable":"",
"Tradeable":""
},
{
"Name":"",
"Marketable":"",
"Tradeable":""
}
]
}
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 here
Bilnord
BilnordOP•2y ago
Thanks for yours help Fyren and neSHa gonna close the thread.
Want results from more Discord servers?
Add your server