代码之家  ›  专栏  ›  技术社区  ›  Brad Mathews

使用反序列化GeoJSON坐标数组Json.net

  •  0
  • Brad Mathews  · 技术社区  · 6 年前

    我有一个GeoJSON几何体,看起来像这样:

    {
        "type":"Point",
        "coordinates":[-121.35753,38.49392]
    }
    

    我尝试过这两种类定义:

    Public Class GeoJSONPoint
        Public Property type As String
        Public Property coordinates() As Double
    End Class
    
    Public Class GeoJSONPoint
        Public Property type As String
        Public Property coordinates as List(Of Double)
    End Class
    

    在我弄清楚点之后,我还需要弄清楚多重多边形,它是一个由数组组成的数组,看起来像这样:

    {
        "type":"MultiPolygon",
        "coordinates":[[[[-118.718979785211,34.2749418860063],[-118.71897943847,34.2746561231818],[-118.719144944,34.2746560927904],[-118.719145294476,34.2749418391076],[-118.718979785211,34.2749418860063]]]]
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Brad Mathews    6 年前

    好吧,问题不在于我的类,而在于我如何反序列化JSON。

    即使完整的错误消息使它看起来像是暗示了正确的类型,这也不起作用:

    Point = JsonConvert.DeserializeObject(Site.geoJSON)
    

    您必须明确告诉它类型:

    Point = JsonConvert.DeserializeObject(Of GeoJSONPoint)(Site.geoJSON)
    

    Public Class geoJSONGeometry
        Public Property type As String
        Public Property coordinates As Object
    End Class