爲了您的序列化包裝類,使得其內部字典出現在JSON彷彿包裝都沒有,你需要自定義JsonConverter
。 A JsonConverter
可讓您直接控制對特定類別進行序列化和/或反序列化的內容。
下面是一個適合您情況的轉換器。既然你沒有真正提供關於你的包裝類的任何細節,除了它有一個名爲values
的字段來存放字典,我使用了反射來訪問它。如果你的類有公共方法直接操縱字典,你可以改變轉換器來使用這些方法,如果你願意的話。下面是代碼:
class DictionaryWrapperConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(MyWrapper));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
MyWrapper wrapper = (MyWrapper)value;
FieldInfo field = typeof(MyWrapper).GetField("values", BindingFlags.NonPublic | BindingFlags.Instance);
JObject jo = JObject.FromObject(field.GetValue(wrapper));
jo.WriteTo(writer);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
MyWrapper wrapper = new MyWrapper();
FieldInfo field = typeof(MyWrapper).GetField("values", BindingFlags.NonPublic | BindingFlags.Instance);
field.SetValue(wrapper, jo.ToObject(field.FieldType));
return wrapper;
}
}
扳平自定義轉換到你的包裝類,您可以在[JsonConverter]
屬性添加到類定義:
[JsonConverter(typeof(DictionaryWrapperConverter))]
class MyWrapper : IEnumerable
{
Dictionary<string, string> values = new Dictionary<string, string>();
public void Add(string key, string value)
{
values.Add(key, value);
}
IEnumerator IEnumerable.GetEnumerator()
{
return values.GetEnumerator();
}
}
這裏是在運行中的轉換器的完整演示,第一序列化和反序列化包裝類的一個實例,則串行化和反序列化包裝的列表:
class Program
{
static void Main(string[] args)
{
MyWrapper wrapper = new MyWrapper();
wrapper.Add("foo", "bar");
wrapper.Add("fizz", "bang");
// serialize single wrapper instance
string json = JsonConvert.SerializeObject(wrapper, Formatting.Indented);
Console.WriteLine(json);
Console.WriteLine();
// deserialize single wrapper instance
wrapper = JsonConvert.DeserializeObject<MyWrapper>(json);
foreach (KeyValuePair<string, string> kvp in wrapper)
{
Console.WriteLine(kvp.Key + "=" + kvp.Value);
}
Console.WriteLine();
Console.WriteLine("----------\n");
MyWrapper wrapper2 = new MyWrapper();
wrapper2.Add("a", "1");
wrapper2.Add("b", "2");
wrapper2.Add("c", "3");
List<MyWrapper> list = new List<MyWrapper> { wrapper, wrapper2 };
// serialize list of wrappers
json = JsonConvert.SerializeObject(list, Formatting.Indented);
Console.WriteLine(json);
Console.WriteLine();
// deserialize list of wrappers
list = JsonConvert.DeserializeObject<List<MyWrapper>>(json);
foreach (MyWrapper w in list)
{
foreach (KeyValuePair<string, string> kvp in w)
{
Console.WriteLine(kvp.Key + "=" + kvp.Value);
}
Console.WriteLine();
}
}
}
輸出:
{
"foo": "bar",
"fizz": "bang"
}
foo=bar
fizz=bang
----------
[
{
"foo": "bar",
"fizz": "bang"
},
{
"a": "1",
"b": "2",
"c": "3"
}
]
foo=bar
fizz=bang
a=1
b=2
c=3
在數組上調用'JsonConvert.SerializeObject',**之前調用'ToJson()'。 nett結果將是相同的。 – Liam
http:// stackoverflow。com/questions/17368484/json-net-how-to-override-serialization-for-a-type-that-defining-a-custom-jsonconv – Tewr
@alwayslearning在回答你的後續問題:不,沒有辦法使用'[JsonProperty]'屬性來消除屬性名稱。這就是爲什麼你需要轉換器。 –