簡體   English   中英

如何在 C# 中序列化和反序列化 geojson

[英]How to serialize and deserialize geojson in C#

我正在嘗試將對象反序列化為 json,其中位置詳細信息應轉換為 geojson 格式。 試圖使用 geojson.net nuget 包實現這一點,但我無法實現相同的目標。 網絡中沒有適用於 geojson 的示例。 我的請求對象:

public class Request
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Fence Fence { get; set; }
}
public class Fence
{
    public int Type { get; set; }
    public List<PValues> Values { get; set; }
}

public class PValues
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

我想將請求對象轉換為 json,我可以使用 Newtonsoft 反序列化來實現,但在請求 PValues 內部必須轉換為 geojson 多邊形類型我如何在 c# 中做到這一點?

我是 GeoJson 的新手,但是當我閱讀規范時,多邊形規范如下所示

    {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [80.227249, 12.901617],
            [80.227764, 12.888553],
            [80.232056, 12.89006],
            [80.233086, 12.900779],
            [80.227249, 12.901617]
          ]
        ]
      }
    }
  ]
}

所以當我反序列化請求類時,我需要上面的對象來代替值。

為確保您創建正確的類來映射 json 對象,請使用 Visual Studio 的“特殊粘貼”功能。 你可以用它做的是,創建一個新類,

編輯 > 選擇性粘貼 > 將 JSON 粘貼為類

或者簡單地訪問http://json2csharp.com/並將您的 json 數據放入並單擊生成按鈕...這將為您提供您可以簡單復制的課程。

僅供參考,VS Paste 特別生成以下類...

    class YourClassName
    {

        public class Rootobject
        {
            public string type { get; set; }
            public Feature[] features { get; set; }
        }

        public class Feature
        {
            public string type { get; set; }
            public Properties properties { get; set; }
            public Geometry geometry { get; set; }
        }

        public class Properties
        {
        }

        public class Geometry
        {
            public string type { get; set; }
            public float[][][] coordinates { get; set; }
        }

    }

http://json2csharp.com/將生成以下類...

public class Properties
{
}

public class Geometry
{
    public string type { get; set; }
    public List<List<List<double>>> coordinates { get; set; }
}

public class Feature
{
    public string type { get; set; }
    public Properties properties { get; set; }
    public Geometry geometry { get; set; }
}

public class RootObject
{
    public string type { get; set; }
    public List<Feature> features { get; set; }
}

兩者都可以工作,但請試一試,看看哪個更易於使用。 即使其中之一不起作用,您也可以記住這些選項以備將來參考。

請注意,不同的形狀類型在幾何中具有不同數量的嵌套數組。 所以我指出,例如,需要一個與多面體不同的容器。 您可能需要修改: public List<List<List<double>>> coordinates { get; set; } public List<List<List<double>>> coordinates { get; set; } public List<List<List<double>>> coordinates { get; set; }public List<double> coordinates { get; set; } public List<double> coordinates { get; set; } public List<double> coordinates { get; set; }取決於形狀類型

使用GeoJSON.Net庫修改您的模型如下:

public class Request
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Fence Fence { get; set; }
}
public class Fence
{
    public int Type { get; set; }
    public FeatureCollection Values { get; set; }
}

初始化請求對象:

var polygon = new Polygon(new List<LineString>
{
    new LineString(new List<IPosition>
    {
        new Position(20.236237,39.4116761),
        new Position(20.2363602,39.4115249),
        new Position(20.2365152,39.4110652),
        new Position(20.2364942,39.4104468),
        new Position(20.236237,39.4116761),
    })
});
var featureCollection = new FeatureCollection(new List<Feature>()
{
    new Feature(polygon)
});
var request = new Request()
{
    Id = 1,
    Name = "MyRequest",
    Fence = new Fence()
    {
        Type = 2,
        Values = featureCollection
    }
};

最后序列化對象:

var json = JsonConvert.SerializeObject(request);

為了能夠正確地序列化和反序列化,您的類結構應該類似於:

public class Properties
{
}

public class Geometry
{
    public string type { get; set; }
    public List<List<List<double>>> coordinates { get; set; }
}

public class Feature
{
    public string type { get; set; }
    public Properties properties { get; set; }
    public Geometry geometry { get; set; }
}

public class RootObject
{
    public string type { get; set; }
    public List<Feature> features { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM