【问题标题】:Issue with serializing data using JSON.Net使用 JSON.Net 序列化数据的问题
【发布时间】:2017-04-02 22:18:45
【问题描述】:

我在我的应用程序使用网络API使用剑道调度,以拉动数据从我的数据库。我创建了一个Web API函数,只是硬编码一些数据在那里,以确保剑道计划能读懂我的数据。这是我的API函数的代码:

    [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);
    }

在剑道调度程序没有被示出在日历上任何东西。使用招,我能看到剑道计划在呼唤着我的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文件我有这行: P>

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文件注释掉那些三线和一切工作正常。 P>

所以,后来我把我的关注我的视图模型。我饰我的属性与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连载我的名字正确只为这一个方法或者是有一些其他的属性,我可以在我的视图模型使用,使名称正确序列化或者是有剑道,一个设置将允许剑道使用骆驼情况下格式?

【问题讨论】:

    标签: c# asp.net-mvc kendo-ui json.net kendo-scheduler


    【解决方案1】:

    如果您使用的是 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,这是造成问题的原因。

    【讨论】:

      猜你喜欢
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      相关资源
      最近更新 更多