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

时间:2018-08-06 20:43:19

标签: serialization json.net

我需要使用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 个答案:

答案 0 :(得分:0)

  1. 定义一个类Times

    public class Times
    {
        public string Begin { get; set; }
        public string End { get; set; }
        public string Duration { get; set; }
    }
    
  2. 将JSON反序列化为Dictionary<string, List<Times>>。字典的键将是日期名称,例如MondayTuesday

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

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