Change JSON serialization from camelCase to PascalCase [duplicate]

偶尔善良 提交于 2020-06-29 03:51:16

问题


After migrating my project from Core 2.1. to 2.2. I am having trouble with my Kendo widgets. Fields in the model are specified with PascalCase and the field names returned from the server in the JSON are using camelCase.

I've added DefaultContractResolver in Startup but JSON is still serialized in camelCase. Any workaround here?


services
  .AddMvc(options => options.EnableEndpointRouting = false).SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
  .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());


回答1:


We had a similar problem with Syncfusion expecting PascalCase.

Until now the only solution we found is to create our own

PascalCasePropertyNamesContractResolver : DefaultContractResolver

Therein we just override the ResolvePropertyName to return the key as is.

Unfortunately we have to reference this ContractResolver in each Json-Return, like this:

return Json(new { result = result.Items, count = result.Count }, new JsonSerializerSettings { ContractResolver = new PascalCasePropertyNamesContractResolver () });

If there are better solutions coming up here: welcome and thanks in advance.



来源:https://stackoverflow.com/questions/55416179/change-json-serialization-from-camelcase-to-pascalcase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!