经常在使用过程中出现接口或其他代码中出现序列化过来的数据包字段命名很不规范的场景,但在C#中,又比较介意这种不规范的命名,所以尝试看看能不能通过别名的方式实现。
1:JSON工具:Newtonsoft.Json
2:实现方式:
通过注解:JsonProperty 中的 PropertyName 属性来实现别名
3:举例:
public class Student {
[JsonProperty(PropertyName ="ID")]
public int id;
[JsonProperty(PropertyName ="XName")]
public string name;
}
序列化:
反序列化: