繁体   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