简体   繁体   English

如何使用newtonsoft.json反序列化复杂对象列表

[英]How do I deserialize list of complex objects with newtonsoft.json

I need to deserialize this JSON using Newtonsoft.json: 我需要使用Newtonsoft.json反序列化此JSON:

{
  "Monday": [
    {
      "begin": "08:00:00",
      "end": "17:00:00",
      "duration": "09:00:00"
    }
  ],
  "Tuesday": [
    {
      "begin": "08:00:00",
      "end": "17:00:00",
      "duration": "09:00:00"
    }
  ]
}
  1. Define a class Times 定义一个类Times

     public class Times { public string Begin { get; set; } public string End { get; set; } public string Duration { get; set; } } 
  2. Deserialize the JSON into a Dictionary<string, List<Times>> . 将JSON反序列化为Dictionary<string, List<Times>> The keys of the dictionary will be the day names, eg Monday , Tuesday , etc. 字典的键将是日期名称,例如MondayTuesday等。

     var dict = JsonConvert.DeserializeObject<Dictionary<string, List<Times>>>(jsonString); 

Fiddle: https://dotnetfiddle.net/CRbTZn 小提琴: https : //dotnetfiddle.net/CRbTZn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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