簡體   English   中英

JSON 轉換為 C#

[英]JSON convert to C#

Anyone know how to convert this JSON POSTMAN JSON image to C# class, where I want to create a dictionary with key as Date and values with other atributtes.. Online tool convert it like this Online tool converter JSON to C# but this is not right.

這是我的 JSON:

{
    "2020-08-27": [
        {
            "Duration": 424,
            "End": "13:04",
            "Start": "06:00"
        },
        {
            "Duration": 366,
            "End": "20:00",
            "Start": "13:54"
        }
    ],
    "2020-08-28": [
        {
            "Duration": 427,
            "End": "13:07",
            "Start": "06:00"
        },
        {
            "Duration": 159,
            "End": "20:00",
            "Start": "17:21"
        }
    ],
    "2020-08-31": [
        {
            "Duration": 15,
            "End": "06:15",
            "Start": "06:00"
        },
        {
            "Duration": 111,
            "End": "08:40",
            "Start": "06:49"
        },
        {
            "Duration": 630,
            "End": "20:00",
            "Start": "09:30"
        }
    ]
}

您有一個 json 符合DateTimeList<Item>的字典。 您可以使用以下內容反序列化您的 Json

public class Item
{
    public int Duration { get; set; }
    public string End { get; set; }
    public string Start { get; set; }
}

// and in main,
var obj = JsonConvert.DeserializeObject<Dictionary<DateTime, List<Item>>>(json);

// Use TryGetValue to look up values of a specific Key 
obj.TryGetValue(DateTime.Parse("8/27/2020"), out List<Item> items);
Console.WriteLine(string.Join(", ", items.Select(x => x.Duration)));

// prints
424, 366

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM