Wow, this is a complicated format to work with. However, this should be possible to understand using a custom JsonConverter . Using JObject inside the converter will protect us from most of the heavy lifting. But first, we need to define a couple of classes to deserialize the data.
The first class I'll call Node ; it will contain a list of Value and vr .
class Node { public IList Value { get; set; } public string vr { get; set; } }
The second class is necessary for storing elements for the case of "PN".
class PnItem { public string AlphabeticName { get; set; } }
Here is the code for the converter. The converter can look at the vr property and use this information to create the correct list type for Value .
class NodeConverter : JsonConverter { public override bool CanConvert(Type objectType) { return (objectType == typeof(Node)); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JObject jo = JObject.Load(reader); Node node = new Node(); node.vr = (string)jo["vr"]; if (node.vr == "PN") { node.Value = jo["Value"].ToObject<List<PnItem>>(serializer); } else if (node.vr == "SQ") { node.Value = jo["Value"].ToObject<List<Dictionary<string, Node>>>(serializer); } else { node.Value = jo["Value"].ToObject<List<string>>(serializer); } return node; } public override bool CanWrite { get { return false; } } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } }
Note that for the "SQ" case, we deserialize a List<Dictionary<string, Node>> . This spans a recursive structure. Whenever Json.Net tries to deserialize Node, it will call back to the converter. We use Dictionary to handle the fact that property names may vary (for example, "00080070", "00080090", etc.). In the root, we must also deserialize to Dictionary<string, Node> for the same reason.
So, to bundle it all together, here is how you would deserialize your JSON:
var dict = JsonConvert.DeserializeObject<Dictionary<string, Node>>(json, new NodeConverter());
Here is a demo: https://dotnetfiddle.net/hsFlxU