0

Здравствуйте. У меня возникла проблема с написанием класса для дочерних элементов с непостоянным именем. Например: "1104574111_188530139" или "2785989080". Проблема в том, что имена этих элементов часто разная, а в названии требуется указать строгое имя. Как можно реализовать класс(c#) для этих элементов?

Имеется json ответ на подобии это-го:

{
  "success": true,
  "rgInventory": {
    "2785989080": {
      "id": "2785989080",
      "classid": "1104574111",
      "instanceid": "188530139",
      "amount": "1",
      "pos": 1
    }
  },
  "rgCurrency": [],
  "rgDescriptions": {
    "1104574111_188530139": {
      "appid": "730",
      "classid": "1104574111",
      "instanceid": "188530139",
      "icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09G3h5SOhe7LPr7Vn35cpsEl0-2Xrdii3APt-RI4ZG71IdOXelJoZVDX_li7kOu-1MW6uZ_JyHV9-n51hRUaMfs",
      "icon_url_large": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpot7HxfDhjxszJemkV09G3h5SOhe7LPr7Vn35c18lwmO7Eu4ih0VDi80drZ276JtfBdQE4ZA3S8gXoxebogZ-57ZiYmCFlvyIi5HjD30vgrWhS6dA",
      "icon_drag_url": "",
      "name": "AK-47 | Elite Build",
      "market_hash_name": "AK-47 | Elite Build (Field-Tested)",
      "market_name": "AK-47 | Elite Build (Field-Tested)",
      "name_color": "D2D2D2",
      "background_color": "",
      "type": "Mil-Spec Grade Rifle",
      "tradable": 0,
      "marketable": 1,
      "commodity": 0,
      "market_tradable_restriction": "7",
      "cache_expiration": "2015-07-14T07:00:00Z",
      "descriptions": [
        {
          "type": "html",
          "value": "Exterior: Field-Tested"
        },
        {
          "type": "html",
          "value": " "
        },
        {
          "type": "html",
          "value": "Powerful and reliable, the AK-47 is one of the most popular assault rifles in the world. It is most deadly in short, controlled bursts of fire. It has been custom painted using a combination of hydrographics and careful stenciling and detailed with heat-transfer decals.\n\n<i>Tread softly Naomi...people in your line of work aren't known for their longevity - The Shield and The Serpent Part 2</i>"
        },
        {
          "type": "html",
          "value": " "
        },
        {
          "type": "html",
          "value": "The Chroma 2 Collection",
          "color": "9da1a9",
          "app_data": {
            "def_index": "65535",
            "is_itemset_name": 1
          }
        },
        {
          "type": "html",
          "value": " "
        }
      ],
      "owner_descriptions": [
        {
          "type": "html",
          "value": " "
        },
        {
          "type": "html",
          "value": "Tradable After Jul 14, 2015 (7:00:00) GMT",
          "color": "ff4040"
        }
      ],
      "actions": [
        {
          "name": "Inspect in Game...",
          "link": "steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20S%owner_steamid%A%assetid%D3189195788429831445"
        }
      ],
      "market_actions": [
        {
          "name": "Inspect in Game...",
          "link": "steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20M%listingid%A%assetid%D3189195788429831445"
        }
      ],
      "tags": [
        {
          "internal_name": "CSGO_Type_Rifle",
          "name": "Rifle",
          "category": "Type",
          "category_name": "Type"
        },
        {
          "internal_name": "weapon_ak47",
          "name": "AK-47",
          "category": "Weapon",
          "category_name": "Weapon"
        },
        {
          "internal_name": "set_community_7",
          "name": "The Chroma 2 Collection",
          "category": "ItemSet",
          "category_name": "Collection"
        },
        {
          "internal_name": "normal",
          "name": "Normal",
          "category": "Quality",
          "category_name": "Category"
        },
        {
          "internal_name": "Rarity_Rare_Weapon",
          "name": "Mil-Spec Grade",
          "category": "Rarity",
          "color": "4b69ff",
          "category_name": "Quality"
        },
        {
          "internal_name": "WearCategory2",
          "name": "Field-Tested",
          "category": "Exterior",
          "category_name": "Exterior"
        }
      ]
    }
  }
  "more": false,
  "more_start": false
}
0

1 ответ 1

2

Это обычный словарь - для него можно использовать Dictionary<string, object>.

Полностью классы для вашего JSON:

public class Inventory
{
    public bool success { get; set; }
    public Dictionary<string, InventoryItem> rgInventory { get; set; }
    public List<object> rgCurrency { get; set; }
    public Dictionary<string, DescriptionItem> rgDescriptions { get; set; }
    public bool more { get; set; }
    public bool more_start { get; set; }
}

public class InventoryItem
{
    public string id { get; set; }
    public string classid { get; set; }
    public string instanceid { get; set; }
    public string amount { get; set; }
    public int pos { get; set; }
}

public class AppData
{
    public string def_index { get; set; }
    public int is_itemset_name { get; set; }
}

public class Description
{
    public string type { get; set; }
    public string value { get; set; }
    public string color { get; set; }
    public AppData app_data { get; set; }
}

public class OwnerDescription
{
    public string type { get; set; }
    public string value { get; set; }
    public string color { get; set; }
}

public class Action
{
    public string name { get; set; }
    public string link { get; set; }
}

public class MarketAction
{
    public string name { get; set; }
    public string link { get; set; }
}

public class Tag
{
    public string internal_name { get; set; }
    public string name { get; set; }
    public string category { get; set; }
    public string category_name { get; set; }
    public string color { get; set; }
}

public class DescriptionItem
{
    public string appid { get; set; }
    public string classid { get; set; }
    public string instanceid { get; set; }
    public string icon_url { get; set; }
    public string icon_url_large { get; set; }
    public string icon_drag_url { get; set; }
    public string name { get; set; }
    public string market_hash_name { get; set; }
    public string market_name { get; set; }
    public string name_color { get; set; }
    public string background_color { get; set; }
    public string type { get; set; }
    public int tradable { get; set; }
    public int marketable { get; set; }
    public int commodity { get; set; }
    public string market_tradable_restriction { get; set; }
    public string cache_expiration { get; set; }
    public List<Description> descriptions { get; set; }
    public List<OwnerDescription> owner_descriptions { get; set; }
    public List<Action> actions { get; set; }
    public List<MarketAction> market_actions { get; set; }
    public List<Tag> tags { get; set; }
}

Для десериализации можно воспользоватся тем-же JSON.NET:

var inventory = JsonConvert.DeserializeObject<Inventory>(json);

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.