This question has already been answered:

{ "success": true, "items": { "\u2605 Bayonet": { "name": "\u2605 Bayonet", "safe_price": "118.31", "safe_net_price": "110.60", "ongoing_price_manipulation": false, "total_volume": 3148, "7_days": { "median_price": "118.31", "median_net_price": "110.60", "average_price": "118.25", "average_net_price": "102.84", "lowest_price": "100.00", "lowest_net_price": "86.97", "highest_price": "132.25", "highest_net_price": "115.00", "mean_absolute_deviation": "5.46", "deviation_percentage": 0.046173361522199, "trend": -0.050389739620763, "volume": 94 }, "30_days": { "median_price": "138.94", "median_net_price": "129.88", "average_price": "135.69", "average_net_price": "118.00", "lowest_price": "56.03", "lowest_net_price": "48.73", "highest_price": "156.60", "highest_net_price": "136.19", "mean_absolute_deviation": "10.81", "deviation_percentage": 0.079666887758862, "trend": -0.23473444256315, "volume": 422 }, "all_time": { "median_price": "148.00", "median_net_price": "138.35", "average_price": "147.33", "average_net_price": "128.12", "lowest_price": "0.17", "lowest_net_price": "0.15", "highest_price": "179.24", "highest_net_price": "155.87", "mean_absolute_deviation": "9.02", "deviation_percentage": 0.061223104595127, "trend": -0.032006931201406, "volume": 3148 }, "first_seen": 1444261666 }, // etc ... "\u2605 Bayonet | Bright Water (Battle-Scarred)": { "name": "\u2605 Bayonet | Bright Water (Battle-Scarred)", "safe_price": "81.21", "safe_net_price": "70.62", "ongoing_price_manipulation": false, "total_volume": 1, "7_days": { "volume": 0 }, "30_days": { "median_price": "81.21", "median_net_price": "70.62", "average_price": "81.21", "average_net_price": "70.62", "lowest_price": "81.21", "lowest_net_price": "70.62", "highest_price": "81.21", "highest_net_price": "70.62", "mean_absolute_deviation": "0.00", "deviation_percentage": 0, "trend": 0, "volume": 1 }, "all_time": { "median_price": "81.21", "median_net_price": "70.62", "average_price": "81.21", "average_net_price": "70.62", "lowest_price": "81.21", "lowest_net_price": "70.62", "highest_price": "81.21", "highest_net_price": "70.62", "mean_absolute_deviation": "0.00", "deviation_percentage": 0, "trend": 0, "volume": 1 }, "first_seen": 1466590845 }, // etc ... "XM1014 | VariCamo Blue (Well-Worn)": { "name": "XM1014 | VariCamo Blue (Well-Worn)", "safe_price": "0.89", "safe_net_price": "0.84", "ongoing_price_manipulation": false, "total_volume": 763, "7_days": { "median_price": "0.89", "median_net_price": "0.84", "average_price": "0.90", "average_net_price": "0.80", "lowest_price": "0.65", "lowest_net_price": "0.58", "highest_price": "1.08", "highest_net_price": "0.95", "mean_absolute_deviation": "0.09", "deviation_percentage": 0.1, "trend": -0.10752688172043, "volume": 30 }, "30_days": { "median_price": "1.12", "median_net_price": "0.99", "average_price": "1.10", "average_net_price": "0.96", "lowest_price": "0.65", "lowest_net_price": "0.58", "highest_price": "1.51", "highest_net_price": "1.32", "mean_absolute_deviation": "0.16", "deviation_percentage": 0.14545454545455, "trend": -0.32851070467574, "volume": 153 }, "all_time": { "median_price": "1.70", "median_net_price": "1.49", "average_price": "1.71", "average_net_price": "1.50", "lowest_price": "0.03", "lowest_net_price": "0.01", "highest_price": "11.50", "highest_net_price": "10.00", "mean_absolute_deviation": "0.41", "deviation_percentage": 0.23976608187135, "trend": -0.67071929583276, "volume": 763 }, "first_seen": 1444277043 } }, "build_time": 19954, "updated_at": 1467408505 

}

Confused in the moment that in the "items" each new item comes with a new name. At the moment, this is what I have:

  public class 7Days { public string median_price { get; set; } public string median_net_price { get; set; } public string average_price { get; set; } public string average_net_price { get; set; } public string lowest_price { get; set; } public string lowest_net_price { get; set; } public string highest_price { get; set; } public string highest_net_price { get; set; } public string mean_absolute_deviation { get; set; } public double deviation_percentage { get; set; } public double trend { get; set; } public int volume { get; set; } } public class 30Days { public string median_price { get; set; } public string median_net_price { get; set; } public string average_price { get; set; } public string average_net_price { get; set; } public string lowest_price { get; set; } public string lowest_net_price { get; set; } public string highest_price { get; set; } public string highest_net_price { get; set; } public string mean_absolute_deviation { get; set; } public double deviation_percentage { get; set; } public double trend { get; set; } public int volume { get; set; } } public class AllTime { public string median_price { get; set; } public string median_net_price { get; set; } public string average_price { get; set; } public string average_net_price { get; set; } public string lowest_price { get; set; } public string lowest_net_price { get; set; } public string highest_price { get; set; } public string highest_net_price { get; set; } public string mean_absolute_deviation { get; set; } public double deviation_percentage { get; set; } public double trend { get; set; } public int volume { get; set; } } public class Bayonet { public string name { get; set; } public string safe_price { get; set; } public string safe_net_price { get; set; } public bool ongoing_price_manipulation { get; set; } public int total_volume { get; set; } public 7Days 7_days { get; set; } public 30Days 30_days { get; set; } public AllTime all_time { get; set; } public int first_seen { get; set; } } public class BayonetBrightWaterBattleScarred { public string name { get; set; } public string safe_price { get; set; } public string safe_net_price { get; set; } public bool ongoing_price_manipulation { get; set; } public int total_volume { get; set; } public 7_days { get; set; } public 30_days { get; set; } public all_time { get; set; } public int first_seen { get; set; } } public class XM1014VariCamoBlueWellWorn { public string name { get; set; } public string safe_price { get; set; } public string safe_net_price { get; set; } public bool ongoing_price_manipulation { get; set; } public int total_volume { get; set; } public 7_days { get; set; } public 30_days { get; set; } public all_time { get; set; } public int first_seen { get; set; } } public class Items { public Bayonet ★ Bayonet { get; set; } public BayonetBrightWaterBattleScarred ★ Bayonet | Bright Water (Battle-Scarred) { get; set; } public XM1014VariCamoBlueWellWorn XM1014 | VariCamo Blue (Well-Worn) { get; set; } } public class Example { public bool success { get; set; } public Items items { get; set; } public int build_time { get; set; } public int updated_at { get; set; } } 

But I need to deserialize json where there are a lot of objects with different names, tell me how to avoid creating classes with each object name? with Coming all)))

UPD. Redid the classes, here's what happened:

 { public class _7Days { public string median_price { get; set; } public string median_net_price { get; set; } public string average_price { get; set; } public string average_net_price { get; set; } public string lowest_price { get; set; } public string lowest_net_price { get; set; } public string highest_price { get; set; } public string highest_net_price { get; set; } public string mean_absolute_deviation { get; set; } public double deviation_percentage { get; set; } public double trend { get; set; } public int volume { get; set; } } public class _30Days { public string median_price { get; set; } public string median_net_price { get; set; } public string average_price { get; set; } public string average_net_price { get; set; } public string lowest_price { get; set; } public string lowest_net_price { get; set; } public string highest_price { get; set; } public string highest_net_price { get; set; } public string mean_absolute_deviation { get; set; } public double deviation_percentage { get; set; } public double trend { get; set; } public int volume { get; set; } } public class _AllTime { public string median_price { get; set; } public string median_net_price { get; set; } public string average_price { get; set; } public string average_net_price { get; set; } public string lowest_price { get; set; } public string lowest_net_price { get; set; } public string highest_price { get; set; } public string highest_net_price { get; set; } public string mean_absolute_deviation { get; set; } public double deviation_percentage { get; set; } public double trend { get; set; } public int volume { get; set; } } public class Item { public string name { get; set; } public string safe_price { get; set; } public string safe_net_price { get; set; } public bool ongoing_price_manipulation { get; set; } public int total_volume { get; set; } public _AllTime all_time { get; set; } public _7Days _7_days { get; set; } public _30Days _30_days { get; set; } public int first_seen { get; set; } } public class Rootobject { public bool success { get; set; } public Dictionary<string, Item> item { get; set; } public int build_time { get; set; } public int updated_at { get; set; } } } 

Created an extra class that reads json:

 public static Rootobject GetRoot() { string path = @"C:\Users\DB.json"; StreamReader sr = new StreamReader(path); string itemDB = sr.ReadToEnd(); return JsonConvert.DeserializeObject<Rootobject>(itemDB); } 

In my program I call:

 Rootobject itemsDB = PricerApi.GetRoot(); 

but in itemsDB the item = null field, although when reading from a file and deserialization is performed, then in the item field all items are present and I open and view them, everything is exactly as it should be in json, what did I do wrong?

Reported as a duplicate by AK , EvgeniyZ , tym32167 , 0xdb , Andrey NOP c # Jan 1 at 11:29 am

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • @AK @AK I have read this topic, I have the code, if the body json I bring to the form: {"XM1014 | VariCamo Blue (Well-Worn)": { pro-ty "7_days": { pro-ty }, "30_days": { pro-ty }, "all_time": { pro-ty }, pro-ty } } But it does not work when the Dictionary at the top level is nested, i.e. with Dictionary if there are several more properties on the same level public class Rootobject { public bool success public Dictionary<string, Item> item { get; set; } public int build_time public int updated_at } public class Rootobject { public bool success public Dictionary<string, Item> item { get; set; } public int build_time public int updated_at } public class Rootobject { public bool success public Dictionary<string, Item> item { get; set; } public int build_time public int updated_at } - Konstantin Nikishonkov
  • In the answer that I gave as a duplicate, is there a link to the resource that generates the classes - did you use it or are you trying to find the correct answer yourself? - AK
  • @AK I tried to generate classes there, but the answer came without a dictionary, so this method did not suit me, but now I figured out what my mistake was! Thanks for the help and for the useful information! - Konstantin Nikishonkov 1:29 pm

1 answer 1

The cause of the problem is a mismatch of field names and their PropertyName in json

For the items field, the best change is the name of the field:

 public Dictionary<string, Item> items { get; set; } 

However, you have another mismatch - in the _7_days and _30_days fields. Here the replacement is not allowed, because PropertyName starts with a number. This is where the PropertyName declaration in front of the field will help:

  [JsonProperty(PropertyName = "7_days")] public _7Days _7_days { get; set; } [JsonProperty(PropertyName = "30_days")] public _30Days _30_days { get; set; } 
  • Yes, in the end I came to the same conclusion, I fixed the items, but forgot about [JsonProperty(PropertyName = "7_days")] now everything works correctly, thanks for the hint! - Konstantin Nikishonkov
  • @KonstantinNikishonkov if you want to thank the author of the answer, check the box to the left of the answer. - Bulson 1:43 pm