如何return EWS 日历预约为JSON?
How to return EWS calendar appointment as JSON?
我正在尝试使用 EWS Managed 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 的结果。但是,它不会以键值形式 return json 中的数据。我也尝试过使用字典,但是这也产生了关于对象名称已经存在的错误;我添加了 apts.Add("ID", a.Id.tostring())
。如何使用对象名称和值将其转换为 JSON 格式的 return?
好吧,需要说明的是,这并不是关于 EWS 或日历约会,而是关于序列化从 WebApi 返回的对象。
您在 JSON 中没有看到 属性 名称 return 的原因是因为您没有 return 具有属性的对象,您 returned a List<string>
或者如果你愿意,一个字符串数组。正如您在评论中所说,return 值如下所示:
["AAM","A subject","1/5/2015 12:00:00 AM","1/6/2015 12:00:00 AM" .. ]
如果您想要 return 具有属性的对象,您可以 return Appointment
对象而不是 List<string>
,或者如果您需要 return 不止一个对象你可以改成IEnumerable<Appointment>
。如果您不想 return Appointment
对象,因为它包含额外的信息,那么创建您自己的 class 和 return 该对象。
我修改了您的代码以使其更通用,但它看起来像这样:
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();
}
我正在尝试使用 EWS Managed 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 的结果。但是,它不会以键值形式 return json 中的数据。我也尝试过使用字典,但是这也产生了关于对象名称已经存在的错误;我添加了 apts.Add("ID", a.Id.tostring())
。如何使用对象名称和值将其转换为 JSON 格式的 return?
好吧,需要说明的是,这并不是关于 EWS 或日历约会,而是关于序列化从 WebApi 返回的对象。
您在 JSON 中没有看到 属性 名称 return 的原因是因为您没有 return 具有属性的对象,您 returned a List<string>
或者如果你愿意,一个字符串数组。正如您在评论中所说,return 值如下所示:
["AAM","A subject","1/5/2015 12:00:00 AM","1/6/2015 12:00:00 AM" .. ]
如果您想要 return 具有属性的对象,您可以 return Appointment
对象而不是 List<string>
,或者如果您需要 return 不止一个对象你可以改成IEnumerable<Appointment>
。如果您不想 return Appointment
对象,因为它包含额外的信息,那么创建您自己的 class 和 return 该对象。
我修改了您的代码以使其更通用,但它看起来像这样:
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();
}