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

反序列化:

本文介绍如何在 C# 中使用 Newtonsoft.Json 库通过 [JsonProperty] 注解为 JSON 序列化字段设置别名,解决序列化字段命名不规范的问题。

6435

被折叠的 条评论
为什么被折叠?



