I'm trying to deserialize json into a class, but "Не удается преобразовать объект типа "System.Boolean" в тип "Sell_Offers"" error "Не удается преобразовать объект типа "System.Boolean" в тип "Sell_Offers"" I want to access offers

 var rez = new JavaScriptSerializer().Deserialize<Root>(json)); 

-

 { "success": true, "results": [ { "classid": "1812818783", "instanceid": "188530139", "sell_offers": { "best_offer": 324, "offers": [ [ 324, 1 ], [ 444, 2 ], [ 446, 1 ], [ 524, 1 ], [ 528, 2 ], [ 540, 1 ], [ 543, 1 ], [ 554, 1 ], [ 1498, 2 ], [ 1500, 1 ] ], "my_offers": null }, "buy_offers": null, "history": null, "info": { "our_market_instanceid": null, "market_name": "P250 | Iron Clad (Немного поношенное)", "name": "P250 | Iron Clad", "market_hash_name": "P250 | Iron Clad (Minimal Wear)", "rarity": "Армейское качество", "quality": "Немного поношенное", "type": "Пистолет", "mtype": "CSGO_Type_Pistol", "slot": "Обыч." } } ] 

}

The built-in generator in Visual Studio gave the following class code

 public class Root { public bool success { get; set; } public Result[] results { get; set; } } public class Result { public string classid { get; set; } public string instanceid { get; set; } public object sell_offers { get; set; } public object buy_offers { get; set; } public object history { get; set; } public Info info { get; set; } } public class Info { public object our_market_instanceid { get; set; } public string market_name { get; set; } public string name { get; set; } public string market_hash_name { get; set; } public string rarity { get; set; } public string quality { get; set; } public string type { get; set; } public string mtype { get; set; } public string slot { get; set; } } 
  • Did you specifically cut JSON, or do you have such a wrong one? - EvgeniyZ
  • Sorry, it turns out with an error copied. pastebin.com/NXj1eA4K here's the full json - Zevra

1 answer 1

And so, we need to access offers . Let's run your JSON through this site. What we see?

  • We have a certain list of results , in which each object contains a separate sell_offers object.
  • So, we sell_offers look at the very sell_offers , and in it we have the list of offers we need, which consists of an array with a sequential display of numbers.

Let's create all this:

 public class Root { public Result[] results { get; set; } } public class Result { public SellOffers sell_offers { get; set; } } public class SellOffers { public List<List<int>> offers { get; set; } } 

I did not begin to display other data here, I think, by analogy, I’ll do it without any problems. You can read the data as you did. By the way, I advise you to use the JSON.NET library, good, convenient. In general, I personally do not trust the studio converter, I prefer this site, but this is already a matter of tastes.


Well, since we have one object maybe a bool , a list, let's check it. public SellOffers sell_offers { get; set; } public SellOffers sell_offers { get; set; } public SellOffers sell_offers { get; set; } on public dynamic sell_offers { get; set; } public dynamic sell_offers { get; set; } public dynamic sell_offers { get; set; } , SellOffers refuse the SellOffers class and do the following:

 foreach (var item in rez.results) { var itemType = item.sell_offers.GetType(); if (itemType.Equals(typeof(bool))) continue; //Пропускаем все те, что имеют тип Bool foreach (var offer in item.sell_offers["offers"]) { Console.WriteLine($"{offer[0]} - {offer[1]}"); } } 
  • I wrote an example (albeit with JSON.NET, but this is only 1 read line replaced). Also did not go through all the results , and took the first. - EvgeniyZ
  • on full json gives an error. - Zevra
  • "Error converting value False to type 'SellOffers'." - Zevra
  • @Zevra Yeah, but you didn't specify the most important thing in the example. You have some sell_offers of type bool. Do you necessarily use standard tools, or can you use JSON.NET? - EvgeniyZ
  • Standard (in this and a hitch - Zevra