0

I have the following JSON file,

 {
 "NAME": {
    "ABC": {
        "Score": 2, 
        "violations": []
    }, 
    "DEF": {
        "Score": 4, 
        "violations": []
    }, 

    "GHI": {
        "Score": 6, 
        "violations": ["badform"]
    }
  }

I am trying to deserilaize this by creating a class but I am finding it very difficult to construct the class as I am either getting nulls or JSON.net crashing. Could anyone tell me the best way to construct the deserializer?

Morpheus
  • 3,285
  • 4
  • 27
  • 57
  • It would be helpful if you showed us what code you have so far – trousyt Mar 11 '16 at 00:06
  • Possible duplicate of [Deserializing JSON data to C# using JSON.NET](http://stackoverflow.com/questions/2546138/deserializing-json-data-to-c-sharp-using-json-net) – Rob Mar 11 '16 at 00:09

1 Answers1

2

This should work (at least it works here if I add missing “}” to the end of JSON file):

using ParsedData = Dictionary<string, Dictionary<string, A>>;

class A {
    public int Score;
    public string[] violations;
}

var parsed = JsonConvert.DeserializeObject<ParsedData>(…);

Another version:

class A {
    public int Score;
    public string[] violations;
}

class B : Dictionary<string, A> {}
class C : Dictionary<string, B> {}

var parsed = JsonConvert.DeserializeObject<C>(…);
Surfin Bird
  • 488
  • 7
  • 16