1

I have a Json file, with the object serialized correctly, but the problem is that the json has what seems like a dictionary with keys that are strings "0","1" and so on.

Is there any way, not involving writing an own parser, to correctly deserialise these into a list?

"WeaponSlots":{
        "0":{
           "WeaponInstalled":null,
           "AllowedWeaponTypes":{
              "0":{
                 "0":2
              }
           },
           "AllowedWeapons":null
        },
        "1":{
           "WeaponInstalled":null,
           "AllowedWeaponTypes":{
              "0":{
                 "0":2
              }
           },
           "AllowedWeapons":null
        }

Example file: https://pastebin.com/i3LQ3L7j

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

2 Answers2

0

You can use the datatype Dictionary<string, object> to deserialize this..

user230910
  • 2,353
  • 2
  • 28
  • 50
0
static void Main(string[] args)
{
    // load the file.
    var file = File.ReadAllText("Example.json");

    // to generate the 'Example' classes from JSON I used
    // https://app.quicktype.io and changed the name to 'Example'
    var example = JsonConvert.DeserializeObject<Example>(file);

    // select the value of each dictionary entry into a list.
    var sections = example.Sections.Select(x => x.Value).ToList();
}
Creyke
  • 1,887
  • 2
  • 12
  • 16