.net core使用Newtonsoft.Json进行序列化处理解决返回值首字母小写
var response = context.HttpContext.Response; response.ContentType = "application/json"; var setting=new JsonSerializerSettings(); setting.ContractResolver = new CamelCasePropertyNamesContractResolver(); //使用驼峰样式 var result = JsonConvert.SerializeObject(Data,setting); response.WriteAsync(result);
我对返回进行了封装,所以配置只能加在序列化这里,全局加的不起作用。驼峰样式首字母是小写的。
全局加配置
services.AddMvc().AddJsonOptions(options => { //忽略循环引用 options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; //不使用驼峰样式的key options.SerializerSettings.ContractResolver = new DefaultContractResolver(); //设置时间格式 options.SerializerSettings.DateFormatString = "yyyy-MM-dd"; });