C# Newtonsoft自定義JsonConverter的實現(xiàn)
在C#中,我們使用Newtonsoft來對JSON對象或字段串進行序列化和反序列化的操作,但在某些場景下,需要在映射過程中需要對屬性進行更改。
1、JsonProperty
JsonProperty用于指定特定字段的名稱,將一個字段指定為另一個字段,示例如下:
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
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] 來對類的屬性進行標(biāo)記,并指定它的中文名稱,如果沒有標(biāo)記,則使用本身的屬性。
CanConvert:判斷當(dāng)前轉(zhuǎn)換器是否適用于指定的類型 T。
ReadJson:在反序列化時,將 JSON 數(shù)據(jù)轉(zhuǎn)換為 C# 對象。根據(jù) [NameAttribute] 的中文名稱找到對應(yīng)的屬性并賦值。
WriteJson:在序列化時,將 C# 對象轉(zhuǎn)換為 JSON 數(shù)據(jù)。根據(jù) [NameAttribute] 的中文名稱生成對應(yīng)的 JSON 字段。
使用方法:
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); // {"用戶名":"Tom","年齡":20}
json對象:
Name:Tom
Age: 20
到此這篇關(guān)于C# Newtonsoft自定義JsonConverter的實現(xiàn)的文章就介紹到這了,更多相關(guān)C# 自定義JsonConverter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實現(xiàn)動態(tài)創(chuàng)建接口并調(diào)用的實例
這篇文章介紹了C#實現(xiàn)動態(tài)創(chuàng)建接口并調(diào)用,文中通過實例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11