2016-11-18 48 views
1

我在我的應用程序中使用Kendo計劃程序來使用Web Api從我的數據庫中提取數據。我創建了一個Web Api函數,並在其中硬編碼了一些數據,以確保Kendo Scheduler可以讀取我的數據。這裏是我的Api函數的代碼:使用JSON.Net序列化數據的問題

[Route("api/v1/Events/GetPersonalEvents", Name = "ApiEventsGetPersonalEvents")] 
    [HttpGet] 
    public DataSourceResult GetPersonalEvents([System.Web.Http.ModelBinding.ModelBinder(typeof(WebApiDataSourceRequestModelBinder))]DataSourceRequest request) 
    { 
     var q = new ViewModels.Events.EventViewModel(); 
     q.Id = 1; 
     q.Title = "This is a test"; 
     q.Start = DateTime.Now; 
     q.End = DateTime.Now.AddHours(1); 
     q.Description = "Test entry"; 

     var list = new List<ViewModels.Events.EventViewModel>(); 
     list.Add(q); 
     return list.ToDataSourceResult(request); 
    } 

Kendo Scheduler沒有在日曆上顯示任何東西。使用Fiddler,我能夠看到Kendo Scheduler正在調用我的API,並且我的API正在返回數據。這是JSON發送:

{ 
    "data":[ 
     { 
     "id":1, 
     "title":"This is a test", 
     "description":"Test entry", 
     "isAllDay":false, 
     "start":"2016-11-18T15:31:33.1173519-08:00", 
     "end":"2016-11-18T16:31:33.1178524-08:00", 
     "startTimezone":null, 
     "endTimezone":null, 
     "recurrenceRule":null, 
     "recurrenceException":null 
     } 
    ], 
    "total":1, 
    "aggregateResults":null, 
    "errors":null 
} 

一切似乎工作正常。經過進一步調查,我終於明白了我的問題。在我global.asax.cs文件我有這行:

HttpConfiguration config = GlobalConfiguration.Configuration; 
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 
config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false; 

這樣做是它會導致JSON.Net自動C#名稱轉換爲Javascript友好的名稱(例如Title變得titleDescription變得description,等...) ,這是我想要的。然而,顯然,劍道要求名稱像C#(例如Title而不是title)。我通過在我的global.asax.cs文件中評論這三行來驗證這一點,並且一切正常。

那麼,我把我的注意力轉向我的ViewModel。我使用JsonProperty屬性裝飾了我的屬性,並指定了一個特定的名稱。但是,它仍然被序列化爲小寫名稱。這裏是查看模型代碼:

public class EventViewModel : ISchedulerEvent 
{ 
    [JsonProperty(PropertyName = "Id")] 
    public int Id { get; set; } 

    [JsonProperty(PropertyName = "Title")] 
    public string Title { get; set; } 

    [JsonProperty(PropertyName = "Description")] 
    public string Description { get; set; } 

    [JsonProperty(PropertyName = "IsAllDay")] 
    public bool IsAllDay { get; set; } 

    [JsonProperty(PropertyName = "Start")] 
    public DateTime Start { get; set; } 

    [JsonProperty(PropertyName = "End")] 
    public DateTime End { get; set; } 

    [JsonProperty(PropertyName = "StartTimezone")] 
    public string StartTimezone { get; set; } 

    [JsonProperty(PropertyName = "EndTimezone")] 
    public string EndTimezone { get; set; } 

    [JsonProperty(PropertyName = "RecurrenceRule")] 
    public string RecurrenceRule { get; set; } 

    [JsonProperty(PropertyName = "RecurrenceException")] 
    public string RecurrenceException { get; set; } 
} 

所以,現在我沒有想法。那麼有沒有辦法讓Json.Net正確序列化我的名字JUST對於這一個方法還是有一些其他的屬性我可以在我的視圖模型中使用來正確序列化名稱或者在Kendo中有一個設置將允許Kendo使用駝峯格式?

回答

3

如果您正在使用Json.NET 9.0.1或更高版本,您可以通過[JsonObject(NamingStrategyType = typeof(TNamingStrategy))]標誌着它指定一個特定類型naming strategy。這覆蓋了命名策略CamelCasePropertyNamesContractResolver。在你的情況,你想DefaultNamingStrategy

[JsonObject(NamingStrategyType = typeof(DefaultNamingStrategy))] 
public class EventViewModel 
{ 
    public int Id { get; set; } 

    public string Title { get; set; } 

    public string Description { get; set; } 

    public bool IsAllDay { get; set; } 

    public DateTime Start { get; set; } 

    public DateTime End { get; set; } 

    public string StartTimezone { get; set; } 

    public string EndTimezone { get; set; } 

    public string RecurrenceRule { get; set; } 

    public string RecurrenceException { get; set; } 
} 

注意,[JsonProperty("name")]屬性不再需要。

在您的全球合約解析器上,還有一個屬性NamingStrategy。將NamingStrategy.OverrideSpecifiedNames設置爲false還可以防止全球覆蓋[JsonProperty("name")]名稱。對於CamelCasePropertyNamesContractResolver它似乎默認爲true,這是您的問題的原因。

+0

這工作!謝謝... – Icemanind