0
{
  "Documents": {
    "c97bba6f-c8ab-4453-91f8-f663f478910c": {
      "id": "c97bba6f-c8ab-4453-91f8-f663f478910c",
      "name": "name",
      "sys": {
        "c67bfa6f-c8ab-4453-91f8-f663f478910c": {
          "id": "c97bba6f-c8ab-4453-91f8-f663f478910c",
          "name": "sys name"
        },
        "a67bfa6f-c8ab-4453-91f8-f663f478910c": {
          "id": " a67bfa6f-c8ab-4453-91f8-f663f478910c",
          "name": "sys name"
        }
      }
    },
    "f97bba6f-c8ab-4453-91f8-f663f478910c": {
      "id": "c97bba6f-c8ab-4453-91f8-f663f478910c",
      "name": "OZL - B - MECH - AC - Fan Coil Units",
      "sys": {}
    }
  }
}

I've got JSON data in the form above and am having trouble deserializing this into a C# class. The trouble I'm having is that the GUID is used to define the object that the Documents object has rather than the documents object having a list of documents.

I've tried to create a documents object that has a list of object in it and deserialise into this using Newtonsoft. I've also tried a dictionary of string, object in the documents object. None of these work.

Any input or ideas would be most appreciated.

Edit: the biggest issue I'm having is the nesting of Dictionary<GUID, object>. Newtonsoft won't map to the dictionary within the first object. Here's what I have:

public class Documents {
   public dictionary<Guid, System> data { get; set; }
}

public class System {
   public Guid Id { get; set;}
   public string Name { get; set; }
   public dictionary<guid, someOtherobject> otherData { get; set;}
}
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
M Leipper
  • 277
  • 5
  • 20
  • In general, you can use a Dictionary. -- Could you post a valid JSON with a structure that makes sense? – Jimi Apr 08 '21 at 11:08
  • It's a `dictionary < Guid, object>`, as you can see the Id are the same – Drag and Drop Apr 08 '21 at 11:15
  • And btw if you try to anonymised some info make sure the result does not look like a Picaso. That's almost barbarism. If you have a valid Json you can use tool like https://app.quicktype.io/ . That will reconize the Dictionary a generate a nice class – Drag and Drop Apr 08 '21 at 11:24
  • Does this answer your question? [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/questions/21611674/how-to-auto-generate-a-c-sharp-class-file-from-a-json-string) – Drag and Drop Apr 08 '21 at 11:25
  • auto-generation of the class isn't an issue the issue is that the names of the objects are dynamic and that they have a collection of items that are also dynamic. – M Leipper Apr 08 '21 at 11:40
  • the dictionary object worked for the top level and left me with a Dictionary of GUIDS with JSON object some of which also have a similar issue. – M Leipper Apr 08 '21 at 11:43
  • In the case above someOtherobject would not get mapped into the dictionary – M Leipper Apr 08 '21 at 12:16
  • The sample JSON you posted was invalid, so I attempted to fix it as best as I could. If I got it wrong, please edit your post to correct it. You can use an online validator such as https://jsonlint.com/ to check it. – Brian Rogers Apr 08 '21 at 18:02

1 Answers1

1

Based on your JSON, the class structure you need is this:

public class RootObject
{
    public Dictionary<Guid, Document> Documents { get; set; }
}

public class Document
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Dictionary<Guid, Sys> Sys { get; set; }
}

public class Sys
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

Then deserialize into the RootObject class like this:

var root = JsonConvert.DeserializeObject<RootObject>(json);

Here is a working demo: https://dotnetfiddle.net/Uycomw

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300