我怎么能得到最后一个" date"来自JSON文件?

时间:2017-02-22 14:16:12

标签: c# json

我有一个JSON文件。文件中的条目可能不是有序的。

我如何获得最后一个日历日期条目。

JSON文件看起来像

[
    {
        "name": "Vacant",
        "date": "02/6/2017",
        "title": "Division Manager",
        "mondayAM": "Main Campus AM",
        "mondayPM": "Main Campus PM",
        "tuesdayAM": "Main Campus AM",
        "tuesdayPM": "Main Campus PM",
        "wednesdayAM": "Main Campus AM",
        "wednesdayPM": "Main Campus PM",
        "thursdayAM": "Main Campus AM",
        "thursdayPM": "Main Campus PM",
        "fridayAM": "Main Campus AM",
        "fridayPM": "Main Campus PM",
        "saturdayAM": "Main Campus AM",
        "saturdayPM": "Main Campus PM"
    },
    {
        "name": "Vacant",
        "date": "02/13/2017",
        "title": "Division Manager",
        "mondayAM": "Main Campus AM",
        "mondayPM": "Main Campus PM",
        "tuesdayAM": "Main Campus AM",
        "tuesdayPM": "Main Campus PM",
        "wednesdayAM": "Main Campus AM",
        "wednesdayPM": "Main Campus PM",
        "thursdayAM": "Main Campus AM",
        "thursdayPM": "Main Campus PM",
        "fridayAM": "Main Campus AM",
        "fridayPM": "Main Campus PM",
        "saturdayAM": "Main Campus AM",
        "saturdayPM": "Main Campus PM"
    },
    {
        "name": "Vacant",
        "date": "02/20/2017",
        "title": "Division Manager",
        "mondayAM": "Main Campus AM",
        "mondayPM": "Main Campus PM",
        "tuesdayAM": "Main Campus AM",
        "tuesdayPM": "Main Campus PM",
        "wednesdayAM": "Main Campus AM",
        "wednesdayPM": "Main Campus PM",
        "thursdayAM": "Main Campus AM",
        "thursdayPM": "Main Campus PM",
        "fridayAM": "Main Campus AM",
        "fridayPM": "Main Campus PM",
        "saturdayAM": "Main Campus AM",
        "saturdayPM": "Main Campus PM"
    },
    {
        "name": "Vacant",
        "date": "02/20/2018",
        "title": "IT Division Manager",
        "mondayAM": "Main Campus AM",
        "mondayPM": "Main Campus PM",
        "tuesdayAM": "Main Campus AM",
        "tuesdayPM": "Main Campus PM",
        "wednesdayAM": "Main Campus AM",
        "wednesdayPM": "Main Campus PM",
        "thursdayAM": "Main Campus AM",
        "thursdayPM": "Main Campus PM",
        "fridayAM": "Main Campus AM",
        "fridayPM": "Main Campus PM",
        "saturdayAM": "Main Campus AM",
        "saturdayPM": "Main Campus PM"
    }
]

如果有快速找到使用LINQ的方法,请告诉我 该日期的唯一模式是它始终是星期一。

我的CalendarModel.cs文件

class CalendarModel
{
        [JsonProperty("name")]
        public string name { get; set; }

        [JsonProperty("date")]
        public string date { get; set; }

        [JsonProperty("title")]
        public string title { get; set; }

        [JsonProperty("mondayAM")]
        public string mondayAM { get; set; }

        [JsonProperty("mondayPM")]
        public string mondayPM { get; set; }

        [JsonProperty("tuesdayAM")]
        public string tuesdayAM { get; set; }

        [JsonProperty("tuesdayPM")]
        public string tuesdayPM { get; set; }

        [JsonProperty("wednesdayAM")]
        public string wednesdayAM { get; set; }

        [JsonProperty("wednesdayPM")]
        public string wednesdayPM { get; set; }

        [JsonProperty("thursdayAM")]
        public string thursdayAM { get; set; }

        [JsonProperty("thursdayPM")]
        public string thursdayPM { get; set; }

        [JsonProperty("fridayAM")]
        public string fridayAM { get; set; }

        [JsonProperty("fridayPM")]
        public string fridayPM { get; set; }

        [JsonProperty("saturdayAM")]
        public string saturdayAM { get; set; }

        [JsonProperty("saturdayPM")]
        public string saturdayPM { get; set; }

 }

1 个答案:

答案 0 :(得分:0)

您的问题(目前)不包含大量信息,因此这是一种猜测。如果您的问题已更新且实际上是关于JSON处理本身,我很可能会删除此答案。

我将假设您以某种方式将JSON处理为类似于此的对象列表:

public class ScheduleEntry
{
    public string Name
    {
        get;
        set;
    }

    public DateTime DateStamp
    {
        get;
        set;
    }

    // Plus the rest of the properties
}

假设您有某种处理方法,您只需要这样的东西:

private void ProcessSchedule()
{
    List<ScheduleEntry> scheduleList = new List<ScheduleEntry>();

    // [Processing from JSON Done Here]

    // [Done Processing]

    DateTime maxDate = scheduleList.Max(o => o.DateStamp);
}