ASP.NET Core应用程序中如何关掉JSON序列化时自动小写属性首字母的命名规则呢?
3.95K 次浏览
在ASP.NET Core的应用程序开发中,通过Web API按钮器返回的JSON序列化时,无论实体对象的首字母是否是小写的,序列化后的JSON的属性的首字母默认都变成了小写。比如有如下的实体类:
public class Hat
{
public int Id { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public int Count { get; set; }
}
默认返回的实体集合对应的序列化JSON字符串为:
[
{"id":1,"name":"Bowler","color":"black","count":1},
{"id":2,"name":"Fedora","color":"red","count":1},
{"id":3,"name":"Baseball Cap","color":"blue","count":3}
]
现在需要关掉默认的首字母小写的JSON序列化规则,应该如何实现呢?即期望的JSON序列化字符串为:
[
{"Id":1,"Name":"Bowler","Color":"black","Count":1},
{"Id":2,"Name":"Fedora","Color":"red","Count":1},
{"Id":3,"Name":"Baseball Cap","Color":"blue","Count":3}
]