繁体   English   中英

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

[英]How to return EWS calendar appointment as JSON?

我正在尝试使用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中的数据。 我也尝试使用Dictionary,但是这也产生了关于已经存在对象名称的错误。 我添加了apts.Add("ID", a.Id.tostring()) 如何使它以JSON格式返回对象名称和值?

好的,只是要澄清一下,这实际上与EWS或日历约会无关,而是对从WebApi返回的对象进行序列化。

之所以看不到JSON中返回的属性名称,是因为您没有返回带有属性的对象,而是返回了List<string>或(如果愿意)返回了一个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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM