How to serialize and deserialize geojson in C#

旧时模样 提交于 2019-12-04 20:37:20

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,

Edit > Paste Special > Paste JSON as classes

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

    }

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.

To be able to serialize and deserialize properly your class structure should be similar to this:

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

Using the GeoJSON.Net library modify your model as follows :

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

Initialize a request object :

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

And finally serialize the object:

var json = JsonConvert.SerializeObject(request);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!