JSON反序列多态对象的数组

编程入门 行业动态 更新时间:2024-10-27 18:30:05
本文介绍了JSON反序列多态对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用JSON反序列多态性涉及对象的数组的一个问题。我试过记录here和here它的工作非常适合系列化,但都吹起来的反序列化。

I'm having a problem with JSON Deserialization involving an array of polymorphic objects. I've tried the solutions for serialization documented here and here which work great for serialization, but both blow up on deserialization.

我的阶级结构如下:

IDable

[DataContract(IsReference=true)] public abstract class IDable<T> { [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<int> { [DataMember] public string Title { get; set; } [DataMember] public List<Observation> Observations { get; set; } [OnDeserializing] void OnDeserializing(StreamingContext context) { init(); } public ObservationGroup() { init(); } private void init() { Observations = new List<Observation>(); ObservationRecords = new List<ObservationRecord>(); } }

DescriptiveObservation

DescriptiveObservation

[DataContract(IsReference = true)] public class DescriptiveObservation : Observation { protected override ObservationType GetObservationType() { return ObservationType.Descriptive; } }

NoteObservation

NoteObservation

[DataContract(IsReference = true)] public class NoteObservation : Observation { protected override ObservationType GetObservationType() { return ObservationType.Note; } }

NumericObservation

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

ScoredObservation

[DataContract(IsReference = true)] public class ScoredObservation : Observation { [DataMember] public int Value { get; set; } protected override ObservationType GetObservationType() { return ObservationType.Scored; } }

我公正的使用无论是内置的JavaScriptSerializer或Newtonsoft JSON库。

I'm impartial to using either the built in JavaScriptSerializer or the Newtonsoft JSON library.

连载code

var settings = new Newtonsoft.Json.JsonSerializerSettings(); settings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects; Newtonsoft.Json.JsonConvert.SerializeObject(AnInitializedScoresheet, Newtonsoft.Json.Formatting.None, settings);

反序列化code

Deserialization Code

return Newtonsoft.Json.JsonConvert.DeserializeObject(returnedStringFromClient, typeof(Scoresheet)); //Scoresheet contains a list of observationgroups

这是我得到的错误是

无法创建类型ProjectXCommon.DataStores.Observation的一个实例。类型是一个接口或抽象类,不能instantated。

"Could not create an instance of type ProjectXCommon.DataStores.Observation. Type is an interface or abstract class and cannot be instantated."

任何帮助将是非常美联社preciated!

Any help would be much appreciated!

推荐答案

您还没有添加时反序列化的任何设置。你需要用 TypeNameHandling 应用设置设置为对象或所有。

You have not added any settings upon deserialization. You need to apply settings with TypeNameHandling set to Object or All.

这样的:

JsonConvert.DeserializeObject( returnedStringFromClient, typeof(Scoresheet), new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects });

文档: TypeNameHandling设置

更多推荐

JSON反序列多态对象的数组

本文发布于:2023-08-04 00:56:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1291487.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   序列   对象   多态   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!