c#反序列化和编辑JSON键的ID

本文关键字:键的 ID JSON 编辑 反序列化 | 更新日期: 2023-09-27 18:14:27

我需要帮助来反序列化和编辑这个JSON:

{
   "1":{
      "id":1,
      "constant":{
      },
      "common":{
         "scrolls":[
         ],
         "charms":[
         ],
         "drops":[
            {
               "id":199,
               "min":1,
               "max":1
            },
            {
               "id":987,
               "min":1,
               "max":1
            },
            {
               "id":985,
               "min":1,
               "max":1
            }
         ]
      },
      "uncommon":{
         "scrolls":[
         ],
         "charms":[
         ],
         "drops":[
            {
               "id":205,
               "min":1,
               "max":1
            },
            {
               "id":1249,
               "min":1,
               "max":1
            }
         ]
      },
      "rare":{
         "scrolls":[
         ],
         "charms":[
         ],
         "drops":[
            {
               "id":215,
               "min":1,
               "max":1
            },
            {
               "id":1201,
               "min":1,
               "max":1
            },
            {
               "id":1149,
               "min":1,
               "max":1
            }
         ]
      },
      "useRareTable":true
   },
   "2049":{
      "id":2049,
      "constant":{
         "scrolls":[
         ],
         "charms":[
         ],
         "drops":[
            {
               "id":592,
               "min":1,
               "max":1
            }
         ]
      },
      "common":{
         "scrolls":[
         ],
         "charms":[
         ],
         "drops":[
            {
               "id":985,
               "min":1,
               "max":1
            }
         ]
      },
      "uncommon":{
         "scrolls":[
         ],
         "charms":[
         ],
         "drops":[
            {
               "id":1197,
               "min":1,
               "max":1
            },
            {
               "id":1249,
               "min":1,
               "max":1
            }
         ]
      },
      "rare":{
         "scrolls":[
         ],
         "charms":[
         ],
         "drops":[
            {
               "id":1147,
               "min":1,
               "max":1
            },
            {
               "id":1149,
               "min":1,
               "max":1
            }
         ]
      },
      "useRareTable":true
   },
   "2":{
      "id":2,
      "constant":{
      },
      "common":{
         "scrolls":[
         ],
         "charms":[
         ],
         "drops":[
            {
               "id":5281,
               "min":1,
               "max":1
            },
            {
               "id":985,
               "min":1,
               "max":1
            }
         ]
      },
      "uncommon":{
         "scrolls":[
         ],
         "charms":[
         ],
         "drops":[
            {
               "id":5301,
               "min":1,
               "max":1
            },
            {
               "id":1249,
               "min":1,
               "max":1
            }
         ]
      },
      "rare":{
         "scrolls":[
         ],
         "charms":[
         ],
         "drops":[
            {
               "id":5303,
               "min":1,
               "max":1
            },
            {
               "id":1201,
               "min":1,
               "max":1
            },
            {
               "id":1149,
               "min":1,
               "max":1
            }
         ]
      },
      "useRareTable":true
   }
}

这是我的类:

   public class types
    {
        public List<drops> scrolls { get; set; }
        public List<drops> charms { get; set; }
        public List<drops> drops { get; set; }
    }
    public class drops
    {
        public int id { get; set; }
        public int min { get; set; }
        public int max { get; set; }
    }
    public class Definition
    {
        public int Id { get; set; }
        public List<types> constant { get; set; }
        public List<types> common { get; set; }
        public List<types> uncommon { get; set; }
        public List<types> rare { get; set; }
        public bool useRareTable { get; set; }
    }
    public class RootObject
    {
        public Dictionary<string, Definition> Definitions { get; set; }
    }

我试图创建将在列表框中添加每个id的工具。当我点击列表框中的任何索引时,应该会出现信息。这个id是游戏中的怪物id,所以每个怪物都有自己的物品掉落列表。之后,我必须能够编辑或添加值并将其序列化回JSON。

c#反序列化和编辑JSON键的ID

我建议您查看一下Json.NET。例如,您可以使用以下函数和代码来反序列化JSON字符串并访问其中一个对象。

// Deserialize the JSON
Dictionary<string, Definition> root = JsonConvert.DeserializeObject<Dictionary<string, Definition>>(jsonString);
// Find out if an object is available in the dictionary
if (root.ContainsKey("2"))
{
  // Get value for key "2"
  Definition value = root["2"];
}