0

我正在尝试序列化 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
                ]
              ]
            ]
          }
        ]
      }
    }
  ]
}

最后,这是区域对象在运行时的样子的片段: 在此处输入图像描述

4

1 回答 1

2

感谢@FObermaier 的评论,我得以实施解决方案。正如他所提到的,我正在使用使用 System.Text.Json 的 NetTopologySuite.IO.GeoJSON4STJ 包。通过重构我的代码以使用 System.Text.Json.JsonSerializer,我能够让它工作。对于遇到相同问题的任何人,请确保同时传递选项对象,并将 GeoJsonConverter 工厂添加为转换器。这是代码示例:

    var options = new JsonSerializerOptions();

    options.Converters.Add(new NetTopologySuite.IO.Converters.GeoJsonConverterFactory());

    var serialized = JsonSerializer.Serialize(areas, options);
    var stringifiedAreas = serialized.ToString();
于 2021-09-02T10:54:27.347 回答