timmortonni
timmortonni

Reputation: 75

Deserialize JSon string using JSON.NET

I am trying to deserialize the following JSon string so that I can capture the values in a b c d ...

{
   "2012-11-26 20:34:12": {
    "a": 65,
    "b": -1,
    "c": "2012-11-26 20:34:12",
    "d": -1,
    "e": 0,
    "f": -112.3211156215747,
    "g": 33.57955864376957
  }
}

JSonlint says that is valid JSon data, but what kind of class would I create in C# to use the JSON.NET JsonConverter to deserialize it ?

I am going to get more data like this and the key will vary ( currently shown as "2012-11-26 20:34:12" ) which is the part that is confusing me.

Any sample code to get me started would be greatly appreciated

Upvotes: 6

Views: 4631

Answers (1)

L.B
L.B

Reputation: 116118

You don't need any class

var obj = (JObject)JsonConvert.DeserializeObject(json);

var dict = obj.First.First.Children().Cast<JProperty>()
            .ToDictionary(p => p.Name, p =>p.Value);

var dt =  (string)dict["c"];
var d = (double)dict["g"];

Upvotes: 4

Related Questions