问题描述
我正在尝试使用 GeoJSON .codeplex.com/"rel =" nofollow> JSON.net 库.根据类型"(type)属性值,每个要素的几何组成可以是许多不同的类型.
I am trying to deserialize GeoJSON using the JSON.net library. The geometry component of each feature can be of many different types based on the "type" attribute value.
我需要将此GeoJSON的几何组件反序列化为几何对象模型,如下所示:
I need to deserialize the geometry component of this GeoJSON into a geometry object model like so:
public abstract class Geometry { ... }
public class Point : Geometry { ... }
public class LineString : Geometry { ... }
public class Polygon : Geometry { ... }
因此,基于"type"属性的值,它将反序列化为相应的.net具体类型,但可以通过其基本Geometry类进行访问.
So based on the value of the "type" attribute, it will deserialize into the corresponding .net concrete type, but accessed via its base Geometry class.
JSON.net库是否提供与 KnownTypeAttribute 或 XmlElementAttribute 在XML序列化中,可以让我将JSON反序列化为具有一组已知派生类的基类吗?
Does the JSON.net library offer anything similar to the KnownTypeAttribute in WCF or XmlElementAttribute in XML Serialization that allows me to deserialize JSON to a base class with a set of known derived classes?
推荐答案
文档此处显示了此示例:
[JsonObject(MemberSerialization.OptIn)]
public class Person
{
// "John Smith"
[JsonProperty]
public string Name { get; set; }
// "2000-12-15T22:11:03"
[JsonProperty]
[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime BirthDate { get; set; }
// new Date(976918263055)
[JsonProperty]
[JsonConverter(typeof(JavaScriptDateTimeConverter))]
public DateTime LastModified { get; set; }
// not serialized
public string Department { get; set; }
}
这篇关于使用JSON.net将JSON反序列化为.net基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!