0

I need help with deserializing and editing this 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
   }
}

Here are my classes:

   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; }
    }

I'm trying to create tool which will add each id in listbox. When I click on any index in listbox there should appear information. This id is monster id in game so every monster has own item drop list. After that I must be able to edit or add values and serialize it back to JSON.

dbc
  • 104,963
  • 20
  • 228
  • 340
Jesus IYD
  • 26
  • 4
  • Welcome to SO. What research have you done? regards –  Oct 22 '16 at 15:36
  • Welcome to [so], this question contains a lot of irrelevant JSON and it's not clear where a list box comes into it nor what you mean when you say "it should open with selected id with info" – Jeremy Thompson Oct 22 '16 at 15:39
  • Each id should be added in listbox. When i click on any index in listbox there should appear information. This id is monster id in game so every monster has own item drop list. – Jesus IYD Oct 22 '16 at 15:56

1 Answers1

2

I'd recommend you to look into Json.NET. You could for example use the following function and code to deserialize your JSON string and access one of the objects.

// 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"];
}
m__
  • 1,721
  • 1
  • 17
  • 30
  • Already tried but i don't understand how to edit or add values. For example i want to add to id 2 new drop values id 955, min 1, max 1 – Jesus IYD Oct 22 '16 at 15:54
  • @JesusIYD I've updated my example with how you could access one of the subobjects once you've deserialized the JSON. – m__ Oct 22 '16 at 16:02
  • getting error: Object reference not set to an instance of an object. – Jesus IYD Oct 22 '16 at 16:04
  • @JesusIYD _"Object reference not set to an instance of an object"_ - thats a **new question** –  Oct 22 '16 at 16:22
  • @JesusIYD The problem was two-fold. The first was that I expected your classes were correct and they were not. The root object is actually a Dictionary so that's what you are deserializing (see my updated answer). Fixing this error yields another error, this time it is because you have empty arrays in your json. Check http://stackoverflow.com/a/32122812/414817 for solutions to this problem (or just avoid empty arrays) – m__ Oct 22 '16 at 16:26