我需要将一些JSON文件输出到客户端应用程序。所以,我有以下代码
var content = System.IO.File.ReadAllText(this.path);
var model = JsonConvert.DeserializeObject(content, new JsonSerializerSettings() {
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
return Ok(model);
它应该以带有camelCase属性名称的Object返回,但我得到它与JSON文件中的相同(PascalCase)
要提到的,在启动文件中,IMvcBuilder.AddJsonOptions也设置了
services.AddMvc()
.AddJsonOptions(options => {
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
});
并且它完美地工作,其他控制器按预期返回对象。问题仅在于从JSON文件转换的对象。
我该如何解决?
答案 0 :(得分:0)