我尝试使用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格式返回?
答案 0 :(得分:1)
好的,要明确这不是关于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>
,或者如果需要返回多个对象,可以将其更改为{{1 }}。如果您不想返回IEnumerable<Appointment>
对象,因为它包含额外的信息,那么请创建自己的类并返回该对象。
我将您的代码修改为更通用,但它看起来像这样:
Appointment