Ste*_*nar 5 c# json dto geojson asp.net-web-api2
我正在尝试创建类似于此处描述的GeoJSON格式的JSON输出:http: //geojson.org/geojson-spec.html
特别是,我从文本格式的数据源返回文本,并希望以下面的评论显示的格式将我的DTO转换为JSON.我遇到的主要问题是尝试创建没有属性名称的坐标数组[[...]].
码:
/*
Geometry Text Format from database: POLYGON ((319686.3666000003 7363726.7955, 319747.05190000031 7363778.9233, 319700.78849999979 7363832.7814, 319640.10329999961 7363780.6536, 319686.3666000003 7363726.7955))
And we want format:
"geometry": {
"type": "Polygon",
"coordinates": [[
[319686.3666000003, 7363726.795],
[319747.0519000003, 7363778.9233],
[319700.78849999979, 7363832.7814],
[319640.10329999961, 7363780.6536],
[319686.3666000003, 7363726.795]
]]
}
*/
// Strip out everything except the coordinates
var coordRawText = myWkt.Replace("POLYGON ((", "");
coordRawText = coordRawText.Replace("))", "");
coordRawText = coordRawText.Replace(", ", ",");
// Seperate coordinates to iterate through
var coordsArray = coordRawText.Split(',');
var coordsEnumerable = coordsArray.Select(coord => coord.Replace(" ", ","));
// Build list of coordinates
var coordsList = new List<CoordinateDto>();
foreach (var coord in coordsEnumerable)
{
var splt = coord.Split(',');
var x = double.Parse(splt[0]);
var y = double.Parse(splt[1]);
coordsList.Add(new CoordinateDto {X = x, Y = y});
}
myDto.Geometry = new GeometryDto
{
Type = "Polygon",
Coordinates = coordsList
};
Run Code Online (Sandbox Code Playgroud)
以上输出"几乎"我想要的,但不完全.输出如下图所示:
"geometry":{"type":"Polygon","coordinates":[{"x":319686.3666000003,"y":7363726.7955},{"x":319747.05190000031,"y":7363778.9233},{"x":319700.78849999979,"y":7363832.7814},{"x":319640.10329999961,"y":7363780.6536},{"x":319686.3666000003,"y":7363726.7955}]}
Run Code Online (Sandbox Code Playgroud)
我的DTO定义如下:
[DataContract]
public class GeometryDto
{
[DataMember]
public string Type { get; set; }
[DataMember]
public List<CoordinateDto> Coordinates { get; set; }
}
[DataContract]
public class CoordinateDto
{
[DataMember]
public double X { get; set; }
[DataMember]
public double Y { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我试图使用元组而不是坐标类,但只是插入"item1"和"item2"属性名称而不是"x"和"y".
我还没有尝试过的唯一事情就是创建自己的JSON转换器?
在此先感谢您的帮助,
亲切的问候,
斯特凡
更新解决方案
我得到了一个解决方案,这要归功于这里(来自Dhanuka777)关于多维数组的选定答案,但为了完整性,以防万一:
我不得不创建一个新的辅助函数(从这里稍微修改版本的Jon Skeet的Create Rectangular Array函数: 如何将数组列表转换为多维数组)
代码解决方案如下图所示:
/*
Geometry Text Format from database: POLYGON ((319686.3666000003 7363726.7955, 319747.05190000031 7363778.9233, 319700.78849999979 7363832.7814, 319640.10329999961 7363780.6536, 319686.3666000003 7363726.7955))
And we want format:
"geometry": {
"type": "Polygon",
"coordinates": [[
[319686.3666000003, 7363726.795],
[319747.0519000003, 7363778.9233],
[319700.78849999979, 7363832.7814],
[319640.10329999961, 7363780.6536],
[319686.3666000003, 7363726.795]
]]
}
*/
// Strip out everything except the coordinates
var coordRawText = myWkt.Replace("POLYGON ((", "");
coordRawText = coordRawText.Replace("))", "");
coordRawText = coordRawText.Replace(", ", ",");
// Seperate coordinates to iterate through
var coordsArray = coordRawText.Split(',');
var coordsEnumerable = coordsArray.Select(coord => coord.Replace(" ", ","));
// Build list of coordinates
var coordsList = new List<double[,]>();
foreach (var coord in coordsEnumerable)
{
var splt = coord.Split(',');
var x = double.Parse(splt[0]);
var y = double.Parse(splt[1]);
coordsList.Add(new[,] {{ x, y }});
}
myDto.Geometry = new GeometryDto
{
Type = "Polygon",
Coordinates = CreateRectangularArray(coordsList)
};
Run Code Online (Sandbox Code Playgroud)
还有一个稍微修改过的Create Rectangular Array定义,如下所示:
static T[,] CreateRectangularArray<T>(IList<T[,]> arrays)
{
// TODO: Validation and special-casing for arrays.Count == 0
int minorLength = arrays[0].Length;
T[,] ret = new T[arrays.Count, minorLength];
for (int i = 0; i < arrays.Count; i++)
{
var array = arrays[i];
if (array.Length != minorLength)
{
throw new ArgumentException
("All arrays must be the same length");
}
for (int j = 0; j < minorLength; j++)
{
ret[i, j] = array[0, j];
}
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)
并更新的GeometryDto如下:
[DataContract]
public class GeometryDto
{
[DataMember]
public string Type { get; set; }
[DataMember]
public double[,] Coordinates { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
Web API将使用Newtonsoft Json以所需格式序列化对象.
我宁愿使用 Newtonsoft Json 序列化器来得到这个输出。将坐标定义为二维数组就可以了。
public class GeometryDto
{
public string Type { get; set; }
public double[,] coordinates { get; set; }
}
class Program
{
static void Main()
{
var obj = new GeometryDto
{
Type = "Polygon",
coordinates = new double[,] { { 319686.3666000003, 7363726.795 }, { 319747.0519000003, 7363778.9233 }, { 5.3434444, 6.423443 }, { 7.2343424234, 8.23424324 } }
};
var json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
Console.WriteLine(json);
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
从这里获取Nuget 。
输出:{“类型”:“多边形”,“坐标”:[[319686.3666000003,7363726.795],[319747.05190000031,7363778.9233],...]}