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

5

我将尝试将一个对象反序列化为JSON格式,其中位置信息应该转换为GeoJSON格式。我尝试使用geojson.net NuGet包来实现这一点,但是我无法做到。目前在.NET中没有可用的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; }
}

我希望将Request对象转换为JSON。我可以使用Newtonsoft反序列化实现这一目标,但是Request 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 是无效的。 - Nitin Sawant
已更新问题,附上了有效的GeoJSON规范。 - user3625533
4个回答

13
为确保您创建的类与 JSON 对象相匹配,请使用 Visual Studio 的“粘贴特殊”功能。您可以使用此功能创建一个新类,

编辑 > 粘贴特殊 > 将 JSON 作为类粘贴

或者只需访问http://json2csharp.com/,将您的 JSON 数据放入并单击生成按钮...然后会给您提供一个类,您可以简单地复制它。

顺便说一句,VS Paste Special 会生成下面这个类...

    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; }
}

两者都可以,但尝试一下并看看哪个更易于使用。即使其中之一不起作用,您也可以将这些选项记在心中以备将来参考。


1
http://json2csharp.com/ 这很酷。谢谢分享 :) - Salman Saleh

4
请注意,不同形状类型在几何形状中具有不同数量的嵌套数组。例如,点需要与多边形不同的容器。您可能需要根据形状类型修改以下代码:public List<List<List<double>>> coordinates { get; set; },改为public List<double> coordinates { get; set; }

1
使用 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);

-1
为了能够正确地序列化和反序列化,您的类结构应该类似于以下内容:
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; }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接