简体   繁体   中英

Deserialize JSon string using JSON.NET

I am trying to deserialize the following JSon string so that I can capture the values in abcd ...

{
   "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

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"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM