如何在C#中序列化和反序列化geojson [英] How to serialize and deserialize geojson in C#

查看:180
本文介绍了如何在C#中序列化和反序列化geojson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在尝试将对象反序列化为json,其中位置详细信息应转换为geojson格式.尝试使用geojson.net nuget软件包实现此目的,但无法实现相同目的. net中没有适用于geojson的示例. 我的请求对象:

Am trying to Deserialize an object to json where the location details should be converted to geojson format. Tried to achieve this using geojson.net nuget package but am not able to acheve the same. There are no examples available for geojson in net. My Object from request:

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

我想使用Newtonsoft反序列化将Request对象转换为json,但是我必须在Request PValues内部将其转换为geojson多边形类型,如何在c#中做到这一点?

I want to convert the Request object to json that i can achieve using Newtonsoft deserialization but inside the Request PValues has to be converted to geojson polygon type how can i do this in c#?

是GeoJson的新手,但是当我阅读规范时,多边形规范如下所示

Am new to GeoJson but when i read the specification the polygon specification is something like below

    {
  "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]
          ]
        ]
      }
    }
  ]
}

因此,当我反序列化请求类时,我需要上面的对象.

So in place of values i need the above object when i deserialize Request Class.

推荐答案

为确保创建正确的类以针对json对象进行映射,请使用Visual Studio的选择性粘贴"功能.您可以做的就是创建一个新类

To ensure you create the right class to map against json object, use 'paste special' feature of visual studio. What you could do with that is, create a new class,

编辑>选择性粘贴>将JSON作为类粘贴

Edit > Paste Special > Paste JSON as classes

或者直接访问 http://json2csharp.com/,然后将json数据放入其中,然后点击生成按钮. ..这将为您提供可以简单复制的课程.

Or simply visit http://json2csharp.com/ and chuck you json data in and click generate button...which will then give you class that you could simply copy.

FYI,VS Paste special在类下生成...

FYI, VS Paste special generates below class...

    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/会在下面的类中生成...

And http://json2csharp.com/ will generate below class...

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

两者都可以工作,但让他们去看看,这将使您更易于使用.即使其中之一不起作用,您也可以牢记这些选项以备将来参考.

Both may work but give them a go and see which gives you more ease of use. Even one of these doesn't work, you could keep these options in mind for future reference.

这篇关于如何在C#中序列化和反序列化geojson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆