use*_*533 3 c# geojson geojson.net
我正在尝试将对象反序列化为 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; }
}
Run Code Online (Sandbox Code Playgroud)
我想将请求对象转换为 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]
]
]
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
所以当我反序列化请求类时,我需要上面的对象来代替值。
为确保您创建正确的类来映射 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; }
}
}
Run Code Online (Sandbox Code Playgroud)
而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; }
}
Run Code Online (Sandbox Code Playgroud)
两者都可以工作,但请试一试,看看哪个更易于使用。即使其中之一不起作用,您也可以记住这些选项以备将来参考。