Issue with serializing data using JSON.Net

后端 未结 1 859
小鲜肉
小鲜肉 2021-01-25 16:00

I am using a Kendo Scheduler in my app to pull data from my database using a Web Api. I created a Web Api function and just hard coded some data in there to be sure the Kendo Sc

相关标签:
1条回答
  • 2021-01-25 16:44

    If you are using Json.NET 9.0.1 or later, you can specify a naming strategy for a specific type by marking it with [JsonObject(NamingStrategyType = typeof(TNamingStrategy))]. This overrides the naming strategy of CamelCasePropertyNamesContractResolver. In your case you want 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; }
    }
    

    Note that the [JsonProperty("name")] attributes are no longer needed.

    On your global contract resolver, there is also a property NamingStrategy. Setting NamingStrategy.OverrideSpecifiedNames to false also prevents [JsonProperty("name")] names from being overridden globally. For CamelCasePropertyNamesContractResolver it seems the default is true, which is the cause of your problem.

    0 讨论(0)
提交回复
热议问题