在C#中,我们使用Newtonsoft来对JSON对象或字段串进行序列化和反序列化的操作,但在某些场景下,需要在映射过程中需要对属性进行更改。
1、JsonProperty JsonProperty用于指定特定字段的名称,将一个字段指定为另一个字段,示例如下:
1 2 3 4 5 6 7 8 9 10 public class UserInfo { [JsonProperty("用户名" ) ] public string Name { get ; set ; } [JsonProperty("年龄" ) ] public string Age { get ; set ; } } var tom = new UserInfo{Name = 'Tom' , Age = 20 };var jsonstr = JsonConvert.SerializeObject(tom);var json = JsonConvert.DeserializeObject<UserInfo>(jsonstr);
这时jsonstr的值就是{“用户名”:”Tom”,”年龄”:20}; 但是这会有个问题,json对象的Name和Age都是null; 原因是指定的字段名不匹配,也就是说json字符串中字段名要与JsonProperty的值相同才能正确的映射。
2、自定义JsonConverter 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 public class JsonToChineseConverter <T > : JsonConverter { public override bool CanConvert (Type objectType ) { return objectType == typeof (T); } public override object ReadJson (JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer ) { JObject jsonObject = JObject.Load(reader); T targetObject = (T)Activator.CreateInstance(objectType); foreach (var property in objectType.GetProperties()) { NameAttribute nameAttribute = property.GetCustomAttribute<NameAttribute>(); string propertyName = nameAttribute != null ? nameAttribute.ChineseName : property.Name; if (jsonObject[propertyName] != null ) { property.SetValue(targetObject, jsonObject[propertyName].ToObject(property.PropertyType, serializer)); } } return targetObject; } public override void WriteJson (JsonWriter writer, object value , JsonSerializer serializer ) { JObject jsonObject = new JObject(); Type objectType = value .GetType(); foreach (var property in objectType.GetProperties()) { NameAttribute nameAttribute = property.GetCustomAttribute<NameAttribute>(); string propertyName = nameAttribute != null ? nameAttribute.ChineseName : property.Name; jsonObject[propertyName] = JToken.FromObject(property.GetValue(value ), serializer); } jsonObject.WriteTo(writer); } } [AttributeUsage(AttributeTargets.Property) ] public class NameAttribute : Attribute { public string ChineseName { get ; } public NameAttribute (string chineseName ) { ChineseName = chineseName; } }
说明:
JsonToChineseConverter 是一个泛型的类,继承JsonConverter,用于在 JSON 序列化和反序列化过程中,将 JSON 中的字段名从中文名称映射到 C# 对象的属性名,或者反过来将 C# 对象的属性名映射为中文名称。
该类通过自定义特性 [NameAttribute] 来对类的属性进行标记,并指定它的中文名称,如果没有标记,则使用本身的属性。
CanConvert:判断当前转换器是否适用于指定的类型 T。 ReadJson:在反序列化时,将 JSON 数据转换为 C# 对象。根据 [NameAttribute] 的中文名称找到对应的属性并赋值。 WriteJson:在序列化时,将 C# 对象转换为 JSON 数据。根据 [NameAttribute] 的中文名称生成对应的 JSON 字段。
使用方法:
1 2 3 4 5 6 7 8 9 10 11 12 public class UserInfo { [Name("用户名" ) ] public string Name { get ; set ; } [Name("年龄" ) ] public string Age { get ; set ; } } var tom = new UserInfo{Name = 'Tom' , Age = 20 };var setting = new JsonToChineseConverter<UserInfo>();var jsonstr = JsonConvert.SerializeObject(tom, setting);var json = JsonConvert.DeserializeObject<UserInfo>(jsonstr, setting);Console.WriteLine(jsonstr);
json对象: Name:Tom Age: 20