如何将 EWS 日历约会返回为 JSON

本文关键字:返回 JSON 约会 日历 EWS | 更新日期: 2023-09-27 18:36:00

我正在尝试使用 EWS 托管 API 获取日历事件。我使用了Microsoft在他们的网站上提供的示例。

 public class CalendarController : ApiController
        {
            public static ExchangeService service;
            // GET api/calendar
            public IEnumerable<string> Get()
            {
                // Initialize values for the start and end times, and the number of appointments to retrieve.
                DateTime startDate = DateTime.Now;
                DateTime endDate = startDate.AddDays(30);
                const int NUM_APPTS = 50;
                service = new ExchangeService(ExchangeVersion.Exchange2013_SP1)
                {
                    Credentials = new NetworkCredential("******@example.com", "******"),
                    Url = new Uri("url to ews")
                };
                // Initialize the calendar folder object with only the folder ID. 
                CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());
                // Set the start and end time and number of appointments to retrieve.
                CalendarView cView = new CalendarView(startDate, endDate, NUM_APPTS);
                // Limit the properties returned to the appointment's subject, start time, and end time.
                cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);
                System.Diagnostics.Debug.WriteLine("getting appointments now");
                // Retrieve a collection of appointments by using the calendar view.
                FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);
                System.Diagnostics.Debug.WriteLine(appointments.TotalCount);
                System.Diagnostics.Debug.WriteLine(appointments.ToArray());
                List<string> appts = new List<string>();
                foreach (Appointment a in appointments)
                {
                    appts.Add(a.Id.ToString());
                    appts.Add(a.Subject);
                    appts.Add(a.Start.ToString());
                    appts.Add(a.End.ToString());            
                }
                //var appts = appointments.ToList();
                return appts;
                //return Json(appts, JsonRequestBehavior.AllowGet);
            }

我正在使用WebAPI来获取FullCalendar的结果。但是,它不会以键值形式返回 json 格式的数据。我也尝试使用字典,但这也产生了关于对象名称已经存在的错误;我添加了apts.Add("ID", a.Id.tostring()).如何使其以 JSON 格式返回对象名称和值?

如何将 EWS 日历约会返回为 JSON

好的,需要明确的是,这不是关于 EWS 或日历约会,而是关于序列化从 WebAPI 返回的对象。

您没有看到 JSON 中返回的属性名称的原因是,您没有返回带有属性的对象,而是返回了一个List<string>,或者如果您愿意,还返回了一个字符串数组。正如您在评论中所说,返回值如下所示:

["AAM","A subject","1/5/2015 12:00:00 AM","1/6/2015 12:00:00 AM" .. ]

如果要返回带有属性的对象,可以返回Appointment对象而不是List<string>,或者如果需要返回多个对象,可以将其更改为IEnumerable<Appointment>。如果不想返回 Appointment 对象,因为它包含额外的信息,请创建自己的类并改为返回该对象。

我修改了您的代码以使其更通用,但它看起来像这样:

public IEnumerable<Appointment> Get()
{
    // Initialize values for the start and end times, and the number of appointments to retrieve.
    DateTime startDate = DateTime.Now;
    DateTime endDate = startDate.AddDays(30);
    const int NUM_APPTS = 50;
    // Snipped for brevity
    // Retrieve a collection of appointments by using the calendar view.
    FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);
    return  appointments.AsEnumerable();
}