使用DataContractJsonSerializer使用不同元素类型的数组反序列化JSON

时间:2014-07-07 16:59:12

标签: c# json geojson datacontractserializer datacontractjsonserializer

我想用我的扩展程序反序列化GeoJSON。

GeoJSON的行坐标为

{ coordinates: [[longitude1, latitude1], [longitude2, latitude2, optional_altitude2]] }

这可以超出海拔高度。因此,以下是LineString的有效GeoJSON坐标

{ coordinates: [[longitude1, latitude1], [longitude2, latitude2, optional_altitude2, { my_extension: something_something}]] }

所以,我按如下方式定义我的类:

public class GeoJSonCooridnateExtension
{

    /// <summary>Gets type - should be "Feature".</summary>
    [DataMember(Name = "Something", IsRequired = true)]
    public string Something { get; internal set; }
}

public class GeoJSonGeometry
{
    /// <summary>Gets type - should be "Feature".</summary>
    [DataMember(Name = "type", Order = 0, IsRequired = true)]
    public string GeometryType { get; internal set; }

    /// <summary>Gets horizontal (west-east) position.</summary>
    [DataMember(Name = "coordinates", Order = 1, IsRequired = true)]
    public List<List<object>> Coordinates { get; internal set; }
}

创建序列化程序时,我通过&#34; GeoJSonCooridnateExtension&#34; as&#34;已知类型&#34;。 反序列化器成功但将通用对象放入数组中,而不是我创建的类型。我尝试使用对象API来查询通用对象属性,但都没有返回。甚至可以使用DataContractJSonSerializer来完成它吗?

请参阅下面的示例JSON:

{
    "type": "Feature",        
    "geometry":
    {
        "type": "LineString",          
        "coordinates":             
        [
            [-35.480741083326052,47.926613991573959, null, {"Something":"my-value"}],
            [-34.355741083326052,58.027854137187823],
            [-27.605741083326055,49.776973283701281]
        ]           
    }
}

0 个答案:

没有答案