C# Json信息解析

C# Json信息解析,c#,json,C#,Json,我对json有点陌生,我成功地保存/加载了自己的json文件,到目前为止没有任何问题 但是现在我从一个游戏中得到了一个json文件,其中包含了关于武器的信息。我想我可以循环使用这个json文件,获取我想从中获得的信息 文件如下: { "APIresult": "000", "APImessage": "OK", "Total_Count": 382, "Count": 5, "Weapons": { "Weapons": [{ "id": 3234, "s

我对json有点陌生,我成功地保存/加载了自己的json文件,到目前为止没有任何问题

但是现在我从一个游戏中得到了一个json文件,其中包含了关于武器的信息。我想我可以循环使用这个json文件,获取我想从中获得的信息

文件如下:

{
"APIresult": "000",
"APImessage": "OK",
"Total_Count": 382,
"Count": 5,
"Weapons": {
    "Weapons": [{
        "id": 3234,
        "status": 1,
        "create_date": "2019-03-05 11:28:51.48",
        "update_date": "2019-03-06 14:20:05.427",
        "item_id": "2010192501",
        "item_code": "C1878",
        "image_server": "https://z8games.akamaized.net/cfna/weapon/",
        "image_url_1": "300_169/C1878.png",
        "image_url_2": "100_56/C1878.png",
        "image_url_3": "800_451/C1878.png",
        "database_name": "AK47 Steel Empire",
        "item_index": 0,
        "sale_status": "",
        "item_type": "W",
        "item_category1": "",
        "item_category2": "",
        "item_category3": 1,
        "display_name": "AK-47-Knife-Steampunk",
        "weapon_description": "<p>The AK-47, previously gas-operated, has been completely redesigned to run on steam.\u00a0Hand crafted by master weapon smiths, this latest version will surely be a deadly addition to any arsenal.</p>\r\n",
        "weapon_power": 98,
        "weapon_accuracy": 95,
        "weapon_continuity": 53,
        "weapon_recoil": 76,
        "weapon_weight": 49,
        "weapon_load_ammo": 35,
        "weapon_full_ammo": 105,
        "weapon_range": 0,
        "weapon_angle": 0,
        "add_bullet": 5,
        "special_tag": "vip",
        "featured_weapon": 1,
        "duration": "P",
        "location": "VIPS",
        "currency": "ZP",
        "collection": "",
        "wdn": "201903071878"
    }, {
        "id": 3137,
        "status": 1,
        "create_date": "2018-12-12 02:07:04.707",
        "update_date": "2019-03-06 14:20:47.667",
        "item_id": "2010187401",
        "item_code": "C1827",
        "image_server": "https://z8games.akamaized.net/cfna/weapon/",
        "image_url_1": "300_169/C1827.png",
        "image_url_2": "100_56/C1827.png",
        "image_url_3": "1280_720/C1827.png",
        "database_name": "QBZ95_ArmouredBeast",
        "item_index": 0,
        "sale_status": "",
        "item_type": "W",
        "item_category1": "",
        "item_category2": "",
        "item_category3": 1,
        "display_name": "QBZ-95-Armored Beast",
        "weapon_description": "<p>A unique, but powerful adaptation of the QBZ-95. This weapon has been modified and upgraded with the latest in bio-technological wafare.\u00a0 Unlock the weapon's full potential by equipping it with its IAS and Desert Eagle counterparts! Don't leave spawn without it!</p>\r\n",
        "weapon_power": 71,
        "weapon_accuracy": 95,
        "weapon_continuity": 65,
        "weapon_recoil": 62,
        "weapon_weight": 43,
        "weapon_load_ammo": 35,
        "weapon_full_ammo": 105,
        "weapon_range": 0,
        "weapon_angle": 0,
        "add_bullet": 5,
        "special_tag": "vip",
        "featured_weapon": 0,
        "duration": "P",
        "location": "VIPS",
        "currency": "ZP",
        "collection": "",
        "wdn": "201812121827"
    }, {
        "id": 3065,
        "status": 1,
        "create_date": "2018-09-14 16:57:11.397",
        "update_date": "2018-12-17 11:33:54.067",
        "item_id": "2010168701",
        "item_code": "C1640",
        "image_server": "https://z8games.akamaized.net/cfna/weapon/",
        "image_url_1": "300_169/C1640.png",
        "image_url_2": "100_56/C1640.png",
        "image_url_3": "800_451/C1640.png",
        "database_name": "M4A1-S G Spirit",
        "item_index": 0,
        "sale_status": "",
        "item_type": "W",
        "item_category1": "",
        "item_category2": "",
        "item_category3": 1,
        "display_name": "M4A1-S-Guan Yu",
        "weapon_description": "<p>Inspired by a Chinese hero and his energetic spirit, a famous weapon maker combined the M4A1-S and the Glock 18C into one powerful package.</p>\r\n\r\n<p>In emergency situations, the attacked Glock 18C can be used to attack or the specially plated magazine can be thrown.</p>\r\n",
        "weapon_power": 67,
        "weapon_accuracy": 95,
        "weapon_continuity": 65,
        "weapon_recoil": 60,
        "weapon_weight": 42,
        "weapon_load_ammo": 36,
        "weapon_full_ammo": 108,
        "weapon_range": 0,
        "weapon_angle": 0,
        "add_bullet": 6,
        "special_tag": "vip",
        "featured_weapon": 0,
        "duration": "P",
        "location": "VIPS",
        "currency": "ZP",
        "collection": "",
        "wdn": "201809111640"
    }, {
        "id": 3013,
        "status": 1,
        "create_date": "2018-03-05 09:24:13.25",
        "update_date": "2018-09-14 17:01:10.81",
        "item_id": "2010163101",
        "item_code": "C1584",
        "image_server": "https://crossfire.akamaized.net/cfna/weapon/",
        "image_url_1": "300_169/C1584.png",
        "image_url_2": "100_56/C1584.png",
        "image_url_3": "800_451/C1584.png",
        "database_name": "AN94-Spaceship",
        "item_index": 0,
        "sale_status": "",
        "item_type": "W",
        "item_category1": "",
        "item_category2": "",
        "item_category3": 1,
        "display_name": "AN94-Transformer",
        "weapon_description": "<p>A rare and exotic variant of the AN94 Assault Rifle. This handcrafted masterpiece of weaponry has been designed from rare elements found in outer space, giving it the ability to transform into a deadly Turret to be able to fight supernatural enemies.</p>\r\n",
        "weapon_power": 78,
        "weapon_accuracy": 96,
        "weapon_continuity": 60,
        "weapon_recoil": 55,
        "weapon_weight": 44,
        "weapon_load_ammo": 35,
        "weapon_full_ammo": 105,
        "weapon_range": 0,
        "weapon_angle": 0,
        "add_bullet": 5,
        "special_tag": "vip",
        "featured_weapon": 0,
        "duration": "P",
        "location": "VIPS",
        "currency": "ZP",
        "collection": "",
        "wdn": "201803051584"
    }, {
        "id": 2966,
        "status": 1,
        "create_date": "2017-12-11 13:16:50.637",
        "update_date": "2018-02-16 08:28:07.43",
        "item_id": "2010157801",
        "item_code": "C1532",
        "image_server": "https://z8games.akamaized.net/cfna/weapon/",
        "image_url_1": "300_169/C1532.png",
        "image_url_2": "100_56/C1532.png",
        "image_url_3": "800_451/C1532.png",
        "database_name": "AK-12-Knife-Iron Spider",
        "item_index": 0,
        "sale_status": "",
        "item_type": "W",
        "item_category1": "",
        "item_category2": "",
        "item_category3": 1,
        "display_name": "AK12-Iron Beast",
        "weapon_description": "<p>A rare and exotic variant of the AK-12 Assault Rifle. Its unique materials enliven special senses in its owner.\u00a0 This handcrafted masterpiece of weaponry has been designed with the mysterious qualities of elemental Mercury.</p>\r\n",
        "weapon_power": 96,
        "weapon_accuracy": 97,
        "weapon_continuity": 59,
        "weapon_recoil": 52,
        "weapon_weight": 46,
        "weapon_load_ammo": 35,
        "weapon_full_ammo": 105,
        "weapon_range": 0,
        "weapon_angle": 0,
        "add_bullet": 5,
        "special_tag": "vip",
        "featured_weapon": 0,
        "duration": "P",
        "location": "VIPS",
        "currency": "ZP",
        "collection": "",
        "wdn": "20171211"
    }]
}
}    
我已经明白,我不能只说item.wearm.display_name来获取每个武器的名称,因为它不会通过每个武器id循环,它只是将整个武器块视为一个整体,不会像我习惯的那样循环每个项目


由于我不想创建另一个快速而肮脏的解决方法,我想知道通过json方式归档我的目标的正确方法是什么:)

您可以尝试使用带有泛型类型的
JsonConvert.DeserializeObject

然后使用
JsonProperty
属性标记
JSON
键的名称,因为

var result = JsonConvert.DeserializeObject<RootObject>(File.ReadAllText("info.json"));

感谢您的快速回复,我修复了武器类中命名武器列表的命名问题,因为它无法与两个命名相同的Being一起工作,但它无法从json文件中获得任何信息,foreach返回“null”@Dennis我编辑我的答案并在线编写c#您可以尝试。这看起来很完美!谢谢你,伙计。您是否使用了某种生成器来快速生成这些类?当然有一个网站可以更快地生成类,您只需将json数据放在左侧即可。
var result = JsonConvert.DeserializeObject<RootObject>(File.ReadAllText("info.json"));
public class Weapon
{
    public int id { get; set; }
    public int status { get; set; }
    public string create_date { get; set; }
    public string update_date { get; set; }
    public string item_id { get; set; }
    public string item_code { get; set; }
    public string image_server { get; set; }
    public string image_url_1 { get; set; }
    public string image_url_2 { get; set; }
    public string image_url_3 { get; set; }
    public string database_name { get; set; }
    public int item_index { get; set; }
    public string sale_status { get; set; }
    public string item_type { get; set; }
    public string item_category1 { get; set; }
    public string item_category2 { get; set; }
    public int item_category3 { get; set; }
    public string display_name { get; set; }
    public string weapon_description { get; set; }
    public int weapon_power { get; set; }
    public int weapon_accuracy { get; set; }
    public int weapon_continuity { get; set; }
    public int weapon_recoil { get; set; }
    public int weapon_weight { get; set; }
    public int weapon_load_ammo { get; set; }
    public int weapon_full_ammo { get; set; }
    public int weapon_range { get; set; }
    public int weapon_angle { get; set; }
    public int add_bullet { get; set; }
    public string special_tag { get; set; }
    public int featured_weapon { get; set; }
    public string duration { get; set; }
    public string location { get; set; }
    public string currency { get; set; }
    public string collection { get; set; }
    public string wdn { get; set; }
}

public class Weapons
{
    [JsonProperty("Weapons")]
    public List<Weapon> WeaponList { get; set; }
}

public class RootObject
{
    public string APIresult { get; set; }
    public string APImessage { get; set; }
    public int Total_Count { get; set; }
    public int Count { get; set; }
    [JsonProperty("Weapons")]
    public Weapons Weapon { get; set; }
}
foreach (var w in result.Weapon.WeaponList)
        Console.WriteLine(w.display_name);