JSON使用多态对象数组进行反序列化
我遇到了涉及多态对象数组的JSON反序列化问题。 我已经尝试了这里和这里记录的序列化解决方案,这些解决方案非常适合序列化,但是它们都在反序列化方面有所作为。
我的class级结构如下:
IDable
[DataContract(IsReference=true)] public abstract class IDable { [DataMember] public T ID { get; set; } }
观察组
[DataContract(IsReference=true)] [KnownType(typeof(DescriptiveObservation))] [KnownType(typeof(NoteObservation))] [KnownType(typeof(NumericObservation))] [KnownType(typeof(ScoredObservation))] public class ObservationGroup : IDable { [DataMember] public string Title { get; set; } [DataMember] public List Observations { get; set; } [OnDeserializing] void OnDeserializing(StreamingContext context) { init(); } public ObservationGroup() { init(); } private void init() { Observations = new List(); ObservationRecords = new List(); } }
DescriptiveObservation
[DataContract(IsReference = true)] public class DescriptiveObservation : Observation { protected override ObservationType GetObservationType() { return ObservationType.Descriptive; } }
NoteObservation
[DataContract(IsReference = true)] public class NoteObservation : Observation { protected override ObservationType GetObservationType() { return ObservationType.Note; } }
NumericObservation
[DataContract(IsReference = true)] public class NumericObservation : Observation { [DataMember] public double ConstraintMaximum { get; set; } [DataMember] public double ConstraintMinimum { get; set; } [DataMember] public int PrecisionMaximum { get; set; } [DataMember] public int PrecisionMinimum { get; set; } [DataMember] public string UnitType { get; set; } protected override ObservationType GetObservationType() { return ObservationType.Numeric; } }
ScoredObservation
[DataContract(IsReference = true)] public class ScoredObservation : Observation { [DataMember] public int Value { get; set; } protected override ObservationType GetObservationType() { return ObservationType.Scored; } }
我使用内置的JavaScriptSerializer或Newtonsoft JSON库是公正的。
序列化代码
var settings = new Newtonsoft.Json.JsonSerializerSettings(); settings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects; Newtonsoft.Json.JsonConvert.SerializeObject(AnInitializedScoresheet, Newtonsoft.Json.Formatting.None, settings);
反序列化代码
return Newtonsoft.Json.JsonConvert.DeserializeObject(returnedStringFromClient, typeof(Scoresheet)); //Scoresheet contains a list of observationgroups
我得到的错误是
“无法创建ProjectXCommon.DataStores.Observation类型的实例.Type是一个接口或抽象类,无法立即显示。”
任何帮助将非常感激!
您没有在反序列化时添加任何设置。 您需要将TypeNameHandling
设置为Object
或All
来应用设置。
像这样:
JsonConvert.DeserializeObject( returnedStringFromClient, typeof(Scoresheet), new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects });
文档: TypeNameHandling设置
- ValidationMessageFor与AddModelError(key,message)一起使用。 关键是什么?
- ViewStartPage只能与从WebViewPage或其他ViewStartPage派生的页面一起使用
- 在文件上传时使用CSVHelper
- 将局部视图加载到模态弹出窗口中
- 在基于C#中的存储过程执行查询后,如何为CommandText重用SqlCommand对象?
- 尝试从部分视图中将JS和CSS添加到MVC 3 Razor网站中的布局文件中
- 仅在safari浏览器中ASP.net MVC中的IFrame中的会话为null
- 将querystring从/转换为对象
- 这个基本控制器ASP.NET MVC 3中的自定义主体是非常低效的吗?