C# 将DataTable序列化为GeoJSON对象?
我在将datatable转换为GeoJSON对象时遇到了问题 数据表如下所示:C# 将DataTable序列化为GeoJSON对象?,c#,json,visual-studio-2013,json.net,geojson,C#,Json,Visual Studio 2013,Json.net,Geojson,我在将datatable转换为GeoJSON对象时遇到了问题 数据表如下所示: Name Status imageUrl lat lon Joe Dev markers/Dev.svg 34.21092 -77.59384 Mary Dev markers/Dev.svg 32.49323 -78.43144 { "type" : "FeatureCollec
Name Status imageUrl lat lon
Joe Dev markers/Dev.svg 34.21092 -77.59384
Mary Dev markers/Dev.svg 32.49323 -78.43144
{
"type" : "FeatureCollection",
"features" : [
{
"type" : "Feature",
"properties" : {
"Name " : "Joe",
"Status" : "Dev",
"imageUrl" : "markers/Dev.svg",
"lat" : 34.21092,
"lon" : -77.59384
},
"geometry" : {
"type" : "Point",
"coordinates" : [ -77.59384, 34.21092 ]
}
},
{
"type" : "Feature",
"properties" : {
"Name " : "Mary",
"Status" : "Dev",
"imageUrl" : "markers/Dev.svg",
"lat" : 32.49323,
"lon" : -78.43144
},
"geometry" : {
"type" : "Point",
"coordinates" : [ -78.43144, 32.49323 ]
}
}
]
}
GeoJSON应该如下所示:
Name Status imageUrl lat lon
Joe Dev markers/Dev.svg 34.21092 -77.59384
Mary Dev markers/Dev.svg 32.49323 -78.43144
{
"type" : "FeatureCollection",
"features" : [
{
"type" : "Feature",
"properties" : {
"Name " : "Joe",
"Status" : "Dev",
"imageUrl" : "markers/Dev.svg",
"lat" : 34.21092,
"lon" : -77.59384
},
"geometry" : {
"type" : "Point",
"coordinates" : [ -77.59384, 34.21092 ]
}
},
{
"type" : "Feature",
"properties" : {
"Name " : "Mary",
"Status" : "Dev",
"imageUrl" : "markers/Dev.svg",
"lat" : 32.49323,
"lon" : -78.43144
},
"geometry" : {
"type" : "Point",
"coordinates" : [ -78.43144, 32.49323 ]
}
}
]
}
我到处找,但还没有找到解决办法。我一直在使用NewtonSoft将DataTable
序列化为JSON
对象,但GeoJSON是一种更复杂的格式
最困难的部分是必须在其他类别中包含类别。无论如何,这就是我所尝试的:
使用Newtonsoft
,我能够将数据表转换为JSON。显然,这不是解决方案:
string callback = JsonConvert.SerializeObject(dataTable);
[] resultBytes = Encoding.UTF8.GetBytes(callback);
return new System.IO.MemoryStream(resultBytes);
向前一步,我尝试添加一些geojson属性:
var envelope = new
{
type = "FeatureCollection",
features = result.Tables[0]
};
string callback = JsonConvert.SerializeObject(envelope);
byte[] resultBytes = Encoding.UTF8.GetBytes(callback);
这将返回更接近的结果,但仍然缺少每个数组中的{“type”:“Feature”,“properties”
最后,我在这里发布了一个类似的问题,它非常有用,但是它将实际的json
作为参数,而不是datatable
,这带来了许多其他问题,主要是这个问题
因此,我决定从头开始,发布一个问题,询问如何获取我的源代码(一个常规数据表)并将其转换为类似于上面的GeoJson
对象
非常感谢您的帮助。您需要将
数据表
记录映射到多维对象结构中。我建议使用LinQ
要在dataTable上迭代,请在dataTable对象上使用.AsEnumerable()
解决方案:创建一个函数,将数据表
转换为GeoJSON字符串
确保在.cs
文件中导入System.Linq
命名空间
public static string DataTableToGeoJSONString(DataTable dataTable)
{
var envelope = new
{
type = "FeatureCollection",
features = dataTable.AsEnumerable().Select(record => new {
type = "Feature",
properties = new
{
Name = Convert.ToString(record["Name"]),
Status = Convert.ToString(record["Status"]),
imageUrl = Convert.ToString(record["imageUrl"]),
lat = Convert.ToDecimal(record["lat"]),
lon = Convert.ToDecimal(record["lon"])
},
geometry = new
{
type = "Point",
coordinates = new[] {
Convert.ToDecimal(record["lon"]),
Convert.ToDecimal(record["lat"])
}
}
}).ToArray()
};
return JsonConvert.SerializeObject(envelope);
}
然后从中取出JSON字符串
string geoJson = DataTableToGeoJSONString(dataTable);
请注意,此代码示例使用匿名类new{}
。它可以工作,但我建议使用您必须创建的强类型类
这应该以预期的GeoJSON格式进行序列化。到目前为止,您尝试过什么,有没有一些
c#code
可以共享?我没有发布任何代码认为用户会关注这个问题而不是这个问题,但您是对的。我会发布我尝试过的内容。您看过这篇文章了吗:是的,我看过。它以json对象为参数。像c一样工作伤害,谢谢。