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

C# 如何将EWS日历约会返回为JSON?,c#,json,exchange-server,exchangewebservices,C#,Json,Exchange Server,Exchangewebservices,我正在尝试使用EWS管理的API获取日历事件。我使用了微软在他们网站上提供的例子 public class CalendarController : ApiController { public static ExchangeService service; // GET api/calendar public IEnumerable<string> Get() {

我正在尝试使用EWS管理的API获取日历事件。我使用了微软在他们网站上提供的例子

 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);
            }
公共类日历控制器:ApiController
{
公共静态交换服务;
//获取api/日历
公共IEnumerable Get()
{
//初始化开始和结束时间以及要检索的约会数的值。
DateTime startDate=DateTime.Now;
DateTime endDate=开始日期。添加日期(30);
常数int NUM_APPTS=50;
服务=新的ExchangeService(ExchangeVersion.Exchange2013\u SP1)
{
凭据=新网络凭据(“******@example.com”,“*******”),
Url=新Uri(“ews的Url”)
};
//仅使用文件夹ID初始化日历文件夹对象。
CalendarFolder calendar=CalendarFolder.Bind(服务,WellKnownFolderName.calendar,新属性集());
//设置要检索的开始和结束时间以及约会数。
CalendarView cView=新的CalendarView(开始日期、结束日期、数值应用);
//限制返回到约会主题、开始时间和结束时间的属性。
cView.PropertySet=newpropertyset(appointschema.Subject,appointschema.Start,appointschema.End);
System.Diagnostics.Debug.WriteLine(“立即获取约会”);
//使用日历视图检索约会集合。
FindItemsResults约会=calendar.FindAppoints(cView);
系统.Diagnostics.Debug.WriteLine(约会.TotalCount);
System.Diagnostics.Debug.WriteLine(appointments.ToArray());
List appts=新列表();
foreach(预约中的预约)
{
apps.Add(a.Id.ToString());
附件增补(a.主题);
apps.Add(a.Start.ToString());
apps.Add(a.End.ToString());
}
//var appts=appoints.ToList();
返回应用程序;
//返回Json(appts,JsonRequestBehavior.AllowGet);
}

我正在使用WebAPI获取FullCalendar的结果。但是,它不会以键值形式返回json中的数据。我也尝试使用Dictionary,但这也产生了关于对象名称已经存在的错误;我添加了
apts.Add(“ID”,a.ID.tostring())
。我怎样才能让它以JSON格式返回对象名称和值?

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

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

[“AAM”、“主题”、“2015年1月5日12:00:00 AM”、“2015年1月6日12:00:00 AM”。]

如果要返回具有属性的对象,可以返回
约会
对象,而不是
列表
,或者如果需要返回多个对象,可以将其更改为
IEnumerable
。如果因为包含额外信息而不想返回
约会
对象,请创建自己的对象类并返回该对象

我将您的代码修改为更通用的代码,但看起来是这样的:

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();
}
public IEnumerable Get()
{
//初始化开始和结束时间以及要检索的约会数的值。
DateTime startDate=DateTime.Now;
DateTime endDate=开始日期。添加日期(30);
常数int NUM_APPTS=50;
//剪短
//使用日历视图检索约会集合。
FindItemsResults约会=calendar.FindAppoints(cView);
返回约会。AsEnumerable();
}

你能不能把它序列化成jsonyourself@Sievajet它已经在json中生成了,但它只返回id、subject、start和end dates的值,但它没有常规json中的名称。它是这样产生的:
[“AAM”,“A subject”,“1/5/2015 12:00:00 AM”,“1/6/2015 12:00:00 AM”。]
。第一个应该是ID,然后是subject,依此类推。我已经尝试过了。它给了我错误:
{“message”:“发生了错误”,“exceptionMessage”:“ObjectContent
1”类型未能序列化内容类型“application/json;charset=utf-8”的响应体。”,“exceptionType”:“System.invalidooperationexception”,“stackTrace”:null,“innerException”:{“message”:“发生错误”。“exceptionMessage”:“从“Microsoft.Exchange.WebServices.Data.Appointment”上的“OriginalStart”获取值时出错”。“exceptionType”:“Newtonsoft.Json.JsonSerializationException”",..尝试创建自己的类并复制属性。明白了。这很有效!我花了一段时间才理解你的意思,因为我已经用这种方法尝试过了,但它只保存了最后一个值。我终于发现我做错了什么。它现在运行得很好。谢谢下次,请提问!我很乐意解释r或提供更多代码示例。