无法在 .Net Core 控制器中序列化 GeoJson 坐标
Can't serialize GeoJson coordinates in .Net Core controller
我有一个导出 FeatureCollections 的 .net 6 API 项目。我正在使用 'NetTopologySuite.IO.GeoJSON' 版本 2.0.4,并且有一个 public API 调用,例如:
public async Task<IActionResult> ExportGeoJSON()
{
GeoJSON.Net.Feature.FeatureCollection ret = new GeoJSON.Net.Feature.FeatureCollection();
const double lat = -73.697913;
const double lon = 50.659193;
GeoJSON.Net.Geometry.Position coord = new GeoJSON.Net.Geometry.Position(lat, lon);
GeoJSON.Net.Geometry.Point pt = new GeoJSON.Net.Geometry.Point(coord);
GeoJSON.Net.Feature.Feature feat = new GeoJSON.Net.Feature.Feature(pt, null, Guid.NewGuid().ToString());
ret.Features.Add(feat);
return Ok(ret);
}
当我调用这个时,我只返回:
{
"Type": "FeatureCollection",
"Features": [
{
"Type": "Feature",
"Id": "465f399d-b45c-47ed-b9e6-f395cd86b84b",
"Geometry": {
"Type": "Point"
}
},...
好的,我环顾四周发现了 GeoJSON4STJ https://github.com/NetTopologySuite/NetTopologySuite.IO.GeoJSON,所以我把它放到了我的 Startup 中:
services.AddControllers()
.AddJsonOptions(opts =>
{
opts.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
opts.JsonSerializerOptions.PropertyNamingPolicy = null;
opts.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
opts.JsonSerializerOptions.Converters.Add(new NetTopologySuite.IO.Converters.GeoJsonConverterFactory());
});
和 运行 又一次,但没有任何变化。完全一样的回应。我错过了什么吗?
看来你在混库。 NetTopologySuite.IO.GeoJSON
(和 NetTopologySuite.IO.GeoJSON4STJ
)使用来自 NetTopologySuite.Features
包的功能,而不是来自 GeoJSON.Net
.
我有一个导出 FeatureCollections 的 .net 6 API 项目。我正在使用 'NetTopologySuite.IO.GeoJSON' 版本 2.0.4,并且有一个 public API 调用,例如:
public async Task<IActionResult> ExportGeoJSON()
{
GeoJSON.Net.Feature.FeatureCollection ret = new GeoJSON.Net.Feature.FeatureCollection();
const double lat = -73.697913;
const double lon = 50.659193;
GeoJSON.Net.Geometry.Position coord = new GeoJSON.Net.Geometry.Position(lat, lon);
GeoJSON.Net.Geometry.Point pt = new GeoJSON.Net.Geometry.Point(coord);
GeoJSON.Net.Feature.Feature feat = new GeoJSON.Net.Feature.Feature(pt, null, Guid.NewGuid().ToString());
ret.Features.Add(feat);
return Ok(ret);
}
当我调用这个时,我只返回:
{
"Type": "FeatureCollection",
"Features": [
{
"Type": "Feature",
"Id": "465f399d-b45c-47ed-b9e6-f395cd86b84b",
"Geometry": {
"Type": "Point"
}
},...
好的,我环顾四周发现了 GeoJSON4STJ https://github.com/NetTopologySuite/NetTopologySuite.IO.GeoJSON,所以我把它放到了我的 Startup 中:
services.AddControllers()
.AddJsonOptions(opts =>
{
opts.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
opts.JsonSerializerOptions.PropertyNamingPolicy = null;
opts.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
opts.JsonSerializerOptions.Converters.Add(new NetTopologySuite.IO.Converters.GeoJsonConverterFactory());
});
和 运行 又一次,但没有任何变化。完全一样的回应。我错过了什么吗?
看来你在混库。 NetTopologySuite.IO.GeoJSON
(和 NetTopologySuite.IO.GeoJSON4STJ
)使用来自 NetTopologySuite.Features
包的功能,而不是来自 GeoJSON.Net
.