我正在尝试序列化 FeatureCollection,以便可以将其作为请求正文传递。为此,我使用 NetTopologySuite.IO.GeoJSON 包并使用他们在github 页面上提供的说明。问题是,当点击 jsonSerializer.Serialize 方法时,我收到一条错误消息:{“源数组中的至少一个元素无法转换为目标数组类型。”} System.Exception {System.InvalidCastException}。
我的方法如下所示:
private async Task<IEnumerable<VehicleArea>> GetVehiclesInAreas(FeatureCollection areas)
{
var uri = $"www.someurl/getByArea";
using var httpClient = new HttpClient();
string stringifiedAreas;
var jsonSerializer = GeoJsonSerializer.CreateDefault();
using (var stringWriter = new StringWriter())
using (var jsonwriter = new Newtonsoft.Json.JsonTextWriter(stringWriter))
{
jsonSerializer.Serialize(jsonwriter, areas);
stringifiedAreas = stringWriter.ToString();
}
StringContent content = new StringContent(stringifiedAreas, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(uri, content);
}
我在控制器的请求正文中传递的 GEOSJON 对象,最终成为提供给给定方法的区域 FeatureCollection,如下所示:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": 16,
"geometry": {
"type": "GeometryCollection",
"geometries": [
{
"type": "Polygon",
"coordinates": [
[
[
12.1234,
55.1234
],
[
12.7,
55.7],
[
12.8,
55.8
],
[
12.9,
55.9
],
[
12.1234,
55.1234
]
]
]
}
]
}
},
{
"type": "Feature",
"id": 16,
"geometry": {
"type": "GeometryCollection",
"geometries": [
{
"type": "Polygon",
"coordinates": [
[
[
12.1234,
55.1234
],
[
12.7,
55.7],
[
12.8,
55.8
],
[
12.9,
55.9
],
[
12.1234,
55.1234
]
]
]
}
]
}
}
]
}