将非常Compex和动态Json解析为c#对象

时间:2016-02-23 21:37:45

标签: c# json

我已经读过了。 convert-complex-json-to-c-sharp-class但是没有解决我的问题。

我的json字符串:

{"success":true,"rgInventory":{"4737246963":{"id":"4737246963","classid":"1440503929","instanceid":"188530139","amount":"1","pos":1},"4737246945":{"id":"4737246945","classid":"520025252","instanceid":"0","amount":"1","pos":2}},"rgCurrency":[],"rgDescriptions":{"1440503929_188530139":{"appid":"730","classid":"1440503929","instanceid":"188530139","icon_url":"-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpopL-zJAt21uH3eSR9-9m0h7-GkvP9JrafwjsHvsQmjrmUrI_00FHg_EY-YzzycNeSe1JsZw7R-QS6kry5hMDu6oOJlyWSzPI-Lg","icon_url_large":"-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpopL-zJAt21uH3eSR9-9m0h7-GkvP9JrbummpD78A_3rmXo42ijATh8hA9azz3I4PHclM_NQnX8wXsx7juhsO478-fyCQx7D5iuyhlLV5UlA","icon_drag_url":"","name":"R8 Revolver | Crimson Web","market_hash_name":"R8 Revolver | Crimson Web (Field-Tested)","market_name":"R8 Revolver | Crimson Web (Field-Tested)","name_color":"D2D2D2","background_color":"","type":"Mil-Spec Grade Pistol","tradable":1,"marketable":1,"commodity":0,"market_tradable_restriction":"7","descriptions":[{"type":"html","value":"Exterior: Field-Tested"},{"type":"html","value":" "},{"type":"html","value":"The R8 Revolver delivers a highly accurate and powerful round at the expense of a lengthy trigger-pull. Firing rapidly by fanning the hammer may be the best option when point-blank stopping power is required. It has been painted using a spider web-patterned hydrographic over a red base coat and finished with a semi-gloss topcoat.\n\n<i>Be careful where you walk, you never know where the web is spread<\/i>"},{"type":"html","value":" "},{"type":"html","value":"The Revolver Case Collection","color":"9da1a9","app_data":{"def_index":"65535","is_itemset_name":1}},{"type":"html","value":" "}],"owner_descriptions":"","actions":[{"name":"Inspect in Game...","link":"steam:\/\/rungame\/730\/76561202255233023\/+csgo_econ_action_preview%20S%owner_steamid%A%assetid%D7101653256761565408"}],"market_actions":[{"name":"Inspect in Game...","link":"steam:\/\/rungame\/730\/76561202255233023\/+csgo_econ_action_preview%20M%listingid%A%assetid%D7101653256761565408"}],"tags":[{"internal_name":"CSGO_Type_Pistol","name":"Pistol","category":"Type","category_name":"Type"},{"internal_name":"weapon_revolver","name":"R8 Revolver","category":"Weapon","category_name":"Weapon"},{"internal_name":"set_community_10","name":"The Revolver Case 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"}]},"520025252_0":{"appid":"730","classid":"520025252","instanceid":"0","icon_url":"-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQNqhpOSV-fRPasw8rsUFJ5KBFZv668FFMu1aPMI24auITjxteJwPXxY72AkGgIvZAniLjHpon2jlbl-kpvNjz3JJjVLFG9rl1YLQ","icon_drag_url":"","name":"Operation Breakout Weapon Case","market_hash_name":"Operation Breakout Weapon Case","market_name":"Operation Breakout Weapon Case","name_color":"D2D2D2","background_color":"","type":"Base Grade Container","tradable":1,"marketable":1,"commodity":1,"market_tradable_restriction":"7","descriptions":[{"type":"html","value":" "},{"type":"html","value":"Container Series #18","color":"99ccff"},{"type":"html","value":" "},{"type":"html","value":"Contains one of the following:"},{"type":"html","value":"MP7 | Urban Hazard","color":"4b69ff"},{"type":"html","value":"Negev | Desert-Strike","color":"4b69ff"},{"type":"html","value":"P2000 | Ivory","color":"4b69ff"},{"type":"html","value":"SSG 08 | Abyss","color":"4b69ff"},{"type":"html","value":"UMP-45 | Labyrinth","color":"4b69ff"},{"type":"html","value":"PP-Bizon | Osiris","color":"8847ff"},{"type":"html","value":"CZ75-Auto | Tigris","color":"8847ff"},{"type":"html","value":"Nova | Koi","color":"8847ff"},{"type":"html","value":"P250 | Supernova","color":"8847ff"},{"type":"html","value":"Desert Eagle | Conspiracy","color":"d32ce6"},{"type":"html","value":"Five-SeveN | Fowl Play","color":"d32ce6"},{"type":"html","value":"Glock-18 | Water Elemental","color":"d32ce6"},{"type":"html","value":"P90 | Asiimov","color":"eb4b4b"},{"type":"html","value":"M4A1-S | Cyrex","color":"eb4b4b"},{"type":"html","value":"or an Exceedingly Rare Butterfly Knife!","color":"ffd700"},{"type":"html","value":" "},{"type":"html","value":"","color":"00a000","app_data":{"limited":1}}],"owner_descriptions":"","tags":[{"internal_name":"CSGO_Type_WeaponCase","name":"Container","category":"Type","category_name":"Type"},{"internal_name":"set_community_4","name":"The Breakout Collection","category":"ItemSet","category_name":"Collection"},{"internal_name":"normal","name":"Normal","category":"Quality","category_name":"Category"},{"internal_name":"Rarity_Common","name":"Base Grade","category":"Rarity","color":"b0c3d9","category_name":"Quality"}]}},"more":false,"more_start":false}

我创建了这些类:将json字符串转换为某个对象。

 public class Item
{
    public Int64 id { get; set; }
    public bool success { get; set; }
    public List<rgInventory> rgInventory { get; set; }
    public List<string> rgCurrency { get; set; }

    public List<rgDescription> rgDescription { get; set; } 
    public bool more { get; set; }
    public bool more_start { get; set; }
}

public class rgInventory
{
    public List<rgInventory> rgInventores { get; set; }
    public Int64 id { get; set; }
    public Int64 classid { get; set; }
    public Int64 instanceid { get; set; }
    public Int64 amount { get; set; }
    public Int64 pos { get; set; }
}




public class rgDescription
{
    public Int64 appid { get; set; }
    public Int64 classid { get; set; }
    public Int64 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 bool tradable { get; set; }
    public bool marketable { get; set; }
    public bool commodity { get; set; }
    public int market_tradable_restriction { get; set; }

    public description[] desctiptions { get; set; }

    public string owner_descriptions { get; set; }
    public action[] actions { get; set; }
    public market_action[] market_actions { get; set; }
    public tag[] tags { get; set; }


}

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


}
public class app_data
{
    public int def_index{get;set;}
    public int is_itemset_name{get;set;}
}

public class tag
{
    public string internal_name{get;set;}
    public string name{get;set;}

    public string category{get;set;}
    public string color{get;set;}
    public string category_name{get;set;}
}
public class market_action
{
    public string name{get;set;}
    public string link{get;set;}
}
public class action
{
    public string name{get;set;}
    public string link{get;set;}
}

但是在JSON数据中我认为有一些动态值。我没有定义。请参阅萤火虫的图片。在图片中,如何定义黄色标记的neumaric对象?

enter image description here

2 个答案:

答案 0 :(得分:2)

您应该能够使用字典,具体取决于您使用的解串器。

public class Item {
    public Int64 id { get; set; }
    public bool success { get; set; }
    // ...

    public Dictionary<long, rgDescription> rgDescription { get; set; } 
    // ...
}

基本策略是数字将成为词典中的关键。

答案 1 :(得分:1)

rgInventory,rgCurrency应该是Dictionary&lt; string,rgInventory&gt;和字典&lt; string,rgCurrency&gt; - 你基本上有一个键:值对列表,键是动态条目,值是类类型。所以 - 字典。