将JSON序列化从camelCase更改为PascalCase [英] Change JSON serialization from camelCase to PascalCase

查看:270
本文介绍了将JSON序列化从camelCase更改为PascalCase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Core 2.1迁移我的项目之后.至2.2.我的Kendo小部件出现问题.使用PascalCase指定模型中的字段,并使用camelCase在JSON中从服务器返回的字段名称.

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.

我在Startup中添加了DefaultContractResolver,但JSON仍在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());

推荐答案

我们对Syncfusion期望PascalCase遇到了类似的问题.

We had a similar problem with Syncfusion expecting PascalCase.

直到现在,我们发现的唯一解决方案是创建自己的

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

PascalCasePropertyNamesContractResolver : DefaultContractResolver

其中,我们只是覆盖ResolvePropertyName以按原样返回键.

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

不幸的是,我们必须在每个Json-Return中引用此ContractResolver,如下所示:

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.

这篇关于将JSON序列化从camelCase更改为PascalCase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆