每月的每一天都有动态的类结构举行活动

时间:2019-02-28 09:42:23

标签: c# asp.net-core asp.net-core-webapi

我必须将JSON表示为类结构,因此可以使用它在asp.net核心终结点中返回那些类。我需要返回持有当月每一天的对象,并且每天持有一系列事件。

这里是JSON,其中数字是一个月中的某天。

SimpleLoginActivity

代码如下:

[
  {
    "1":[
      {
        "Title":"Meeting",
        "DateTime":"12:40"
      },
      {
        "Title":"Launch",
        "DateTime":"13:30"
      }
    ],
    "2":[
      {
        "Title":"Test",
        "DateTime":"9:30"
      }
    ]
  }
]

因此,如果有更好的解决方案来实现这一目标,我很高兴知道。

3 个答案:

答案 0 :(得分:2)

一种方法是

1)为一年中的所有月份创建枚举,

public enum Month
{
    NotSet = 0,
    January = 1,
    February = 2,
    March = 3,
    April = 4,
    May = 5,
    June = 6,
    July = 7,
    August = 8,
    September = 9,
    October = 10,
    November = 11,
    December = 12
}

2)创建一个自定义方法,该方法可能需要特定年份的年份超出枚举一个月, 并计算给定月份中的天数,您每天可以填写事件数据

public static MonthEventsResponseModel GetEventData(int year, Month month)
{
    var days = DateTime.DaysInMonth(year, (int)month);

    MonthEventsResponseModel model = new MonthEventsResponseModel();
    model.Events = Enumerable.Range(1, days)
        .Select(day => new
        {
            Day = day,
            Event = new List<Event>()  //<= Instead of new list, Fill Event data with respect to particular day from List or Database Call or API call
        })
        .ToDictionary(x => x.Day, y => y.Event);

    return model;
}

3)然后调用上述方法,

MonthEventsResponseModel model = GetEventData(2018, Month.December);

4)最后将模型转换为json,

string json = JsonConvert.SerializeObject(model);

5)类模型是

public class Event
{
    public string Title { get; set; }
    public string DateTime { get; set; }
}

public class MonthEventsResponseModel
{
    public Dictionary<int, List<Event>> Events { get; set; }
}

答案 1 :(得分:1)

创建一个描述事件的类:

public class Event
{
    public string Title { get; set; }
    public string DateTime { get; set; }
}

然后将您的MonthEventsResponseModel修改为此:

public class MonthEventsResponseModel
{
    public Dictionary<int, List<Event>> Events { get; set; }
}

现在,您可以使用DeserializeObject方法和一些linq反序列化json:

var json = "[\r\n  {\r\n    \"1\":[\r\n      {\r\n        \"Title\":\"Meeting\",\r\n        \"DateTime\":\"12:40\"\r\n      },\r\n      {\r\n        \"Title\":\"Launch\",\r\n        \"DateTime\":\"13:30\"\r\n      }\r\n    ],\r\n    \"2\":[\r\n      {\r\n        \"Title\":\"Test\",\r\n        \"DateTime\":\"9:30\"\r\n      }\r\n    ]\r\n  }\r\n]";

var result = JsonConvert.DeserializeObject<Dictionary<string, Event[]>[]>(json)
    .Select(x => new MonthEventsResponseModel
    {
        Events = x.ToDictionary(k => int.Parse(k.Key), v => v.Value.ToList())
    }).ToArray();

答案 2 :(得分:0)

我将继续制作Event,Day,Month模型,每个模型负责序列化自己-

事件

    public class Event
    {
        public string Title { get; set; }
        public string DateTime { get; set; }

        public string MakeJson()
        {

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder
                .Append("  {").AppendLine()
                .Append("    \"Title\":\"" + Meeting + "\",").AppendLine()
                .Append("    \"DateTime\":\"" + DateTime + "\"").AppendLine()
                .Append("  },").AppendLine();

            return stringBuilder.ToString();
        }
    }

    public class Day
    {
        public string DayNo { get; set; }
        public List<Event> Events { get; set; }

        public string MakeJson()
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("\"" + DayNo + "\":[").AppendLine();    

            foreach (Event ev in Events)
            {
                stringBuilder.Append(ev.MakeJson());
            }

            stringBuilder.AppendLine().Append("],").AppendLine();

            return stringBuilder.ToString();
        }
    }

    public class Month{

        public List<Day> Days { get; set; }

        public string MakeJson()
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("[")
                .AppendLine().Append(" {")
                .AppendLine();

            foreach (Day day in Days)
            {
                stringBuilder.Append(day.MakeJson());
            }

            stringBuilder.Append(" }")
                .AppendLine().Append("]")
                .AppendLine();


        }
    }

一旦填充了模型,只需在当月调用MakeJson(),就可以了。