使用 JSON.Net 序列化数据的问题 [英] Issue with serializing data using JSON.Net
问题描述
我在我的应用程序中使用 Kendo Scheduler 使用 Web Api 从我的数据库中提取数据.我创建了一个 Web Api 函数,并在其中硬编码了一些数据,以确保 Kendo Scheduler 可以读取我的数据.这是我的 Api 函数代码:
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 Scheduler could read my data. Here is my code for the Api function:
[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:
The Kendo Scheduler was not showing anything on the calendar. Using Fiddler, I was able to see that Kendo Scheduler was calling my API and my API was returning data. Here is the JSON getting sent:
{
"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
文件中,我有以下几行:
Everything seemed to be working fine. Upon further investigation, I finally figured out my issue. In my global.asax.cs
file I have these lines:
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
这样做是为了让 JSON.Net 自动将 C# 名称转换为 Javascript 友好的名称(例如 Title
变成 title
、Description
变成 description
等...),这就是我想要的.然而,显然,Kendo 要求名称类似于 C#(例如,Title
而不是 title
).我通过在我的 global.asax.cs
文件中注释掉这三行来验证这一点,一切正常.
What this does is it causes JSON.Net to automatically convert C# names to Javascript-friendly names (e.g. Title
becomes title
, Description
becomes description
, etc...), which is what I want. However, Kendo, apparently, requires the names to be like C# (e.g. Title
instead of title
). I verified this by commenting out those three lines in my global.asax.cs
file and everything worked fine.
于是,我将注意力转向了我的 ViewModel.我用 JsonProperty
属性修饰了我的属性,指定了一个特定的名称.但是,它仍然被序列化为小写名称.这是视图模型代码:
So, then I turned my attention to my ViewModel. I decorated my properties with the JsonProperty
attribute, specifying a specific name. However, it's still being serialized as lower case names. Here is the view model code:
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 正确序列化我的名字,只是为了这个方法,或者我可以在我的视图模型中使用其他一些属性来使名字正确序列化,或者在 Kendo 中有一个设置会允许 Kendo 使用驼峰格式吗?
So now I am out of ideas. So is there a way to either a way to make Json.Net serialize my names properly JUST for this one method or is there some other attribute I can use in my view model to make the names serialize correctly or is there a setting in Kendo that would allow Kendo to use the camel case format?
推荐答案
如果您使用的是 Json.NET 9.0.1 或更高版本,您可以指定一个 命名策略 使用 [JsonObject(NamingStrategyType = typeof(TNamingStrategy))]
.这会覆盖 CamelCasePropertyNamesContractResolver
的命名策略.在您的情况下,您需要 DefaultNamingStrategy
:
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; }
}
请注意,不再需要 [JsonProperty("name")]
属性.
Note that the [JsonProperty("name")]
attributes are no longer needed.
在您的全局合同解析器上,还有一个属性 NamingStrategy
.将 NamingStrategy.OverrideSpecifiedNames
设置为 false
还可以防止 [JsonProperty("name")]
名称被全局覆盖.对于 CamelCasePropertyNamesContractResolver
,似乎默认值为 true
,这就是您的问题的原因.
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.
这篇关于使用 JSON.Net 序列化数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!