要反序列化Dictionary内容中的对像类型键值,需要实现自定义转换器:XXXJsonConverter :JsonConverter
public class xxxJsonConverter : JsonConverter { public override bool CanWrite { get { return false; } } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { string valu = reader.Path; JsonToken jt = reader.TokenType; // Load JObject from stream if (jt== JsonToken.Null) return null; JObject jObject = JObject.Load(reader); Type targetType; JToken typeStr; //JToken varl; if (!jObject.TryGetValue("_t", out typeStr)) { targetType = objectType; } else { var scrName = typeStr.ToString(); if (scrName.Contains(".")) { scrName = scrName.Substring(scrName.LastIndexOf(".", StringComparison.Ordinal) + 1); } targetType = PropertyHintTypeForStr(scrName); } // Displaying output. var target = Activator.CreateInstance(targetType); serializer.Populate(jObject.CreateReader(), target); Dictionaryvtmp = new Dictionary (); foreach (string key in ((PiItem)target).Keys) { if(((PiItem)target).FieldValues[key] is JObject) { vtmp.Add(key, JsonConvert.DeserializeObject (((PiItem)target).FieldValues[key].ToString())); } } foreach (KeyValuePair kv in vtmp) { ((PiItem)target)[kv.Key] = kv.Value; } return target; } public override bool CanConvert(Type objectType) { return objectType == typeof(PiItem); } private Type PropertyHintTypeForStr(string typeStr) { Type result; switch (typeStr) { case nameof(PiItem): result = typeof(PiItem); break; case nameof(ComplexItem): result = typeof(ComplexItem); break; default: result = null; break; } return result; } }