【问题标题】:Serializing FeatureCollection GEOJSON in .NET Core在 .NET Core 中序列化 FeatureCollection GEOJSON
【发布时间】:2021-09-01 11:37:58
【问题描述】:

我正在尝试序列化 FeatureCollection,以便可以将其作为请求正文传递。为此,我使用 NetTopologySuite.IO.GeoJSON 包并使用他们在github page 上提供的说明。问题是,当点击 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
                ]
              ]
            ]
          }
        ]
      }
    }
  ]
}

最后,这里是区域对象在运行时的样子的 sn-p:

【问题讨论】:

  • 您正在传递一个包含StjFeatures 的FeatureCollectionStjFeatures 实现 IFeature 但属于 NetTopologySuite.IO.GeoJSON4STJ 包。该软件包使用System.Text.Json。您可以使用NetTopologySuite.IO.GeoJSON4STJSystem.Text.Json 中的功能重新实现您的代码吗?
  • 谢谢@FObermaier,我设法让它工作了:)

标签: c# .net-core serialization geojson nettopologysuite


【解决方案1】:

感谢@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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 2020-03-24
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多