我有一个Web服务试图返回GeoJson数据,我一直收到这个错误
{“Message”:“发生错误 . ”,“ExceptionMessage”:“'ObjectContent`1'类型无法序列化内容类型'application / json; charset = utf-8'的响应正文 . ”,“ ExceptionType“:”System.InvalidOperationException“,”StackTrace“:null,”InnerException“:{”Message“:”发生错误 . “,”ExceptionMessage“:”方法或操作未实现 . “,”ExceptionType“ :“System.NotImplementedException”,“StackTrace”:“at GeoJSON.Net.Converters.GeometryConverter.WriteJson(JsonWriter writer,Object value,JsonSerializer serializer)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter writer, JsonConverter转换器,Object值,JsonContract Contract ,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty) containerProperty)\ r \ n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue( JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer,IEnumerable values,JsonArrayContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty) )\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Seria在Newtonsoft.Json.JsonSerializer.Serialize中,在Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,Object value,Type objectType)\ r \ n的lization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,Object value,Type objectType)\ r \ n JsonWriter jsonWriter,Object value)\ r \ n在System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type,Object value,Stream writeStream,Encoding effectiveEncoding)\ r \ n在System.Net.Http.Formatting.JsonMediaTypeFormatter . WriteToStream(Type type,Object value,Stream writeStream,Encoding effectiveEncoding)\ r \ n在System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type,Object value,Stream writeStream,HttpContent content)\ r \ n在System . Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type,Object value,Stream writeStream,HttpContent content,TransportContext transportContext,CancellationToken cancellationToken)\ r \ n ---从前一个位置开始的堆栈跟踪异常被抛出--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在System . System.Web.Http.WebHost.HttpControllerHandler.d__14.MoveNext()“}}中的Runtime.CompilerServices.TaskAwaiter.GetResult()\ r \ n
这是我的控制器
// POST api/geo
public List<GeoJSON.Net.Feature.Feature> Post([FromBody] locationsClass loc)
{
var lat = loc.lat;
var lon = loc.lon;
Geo Geo = new Geo();
return Geo.GetRndNearybyLocationList(lat, lon, 400);
}
这是方法GetRndNearybyLocationList
public List<GeoJSON.Net.Feature.Feature> GetRndNearybyLocationList(double lat, double lon, int meters)
{
LocationObject thisRndLocation = new LocationObject();
List<LocationObject> locationsList = new List<LocationObject>();
//List<GeoJSON.Net.Geometry.GeographicPosition> Positions = new List<GeoJSON.Net.Geometry.GeographicPosition>();
Random rnd = new Random();
int dice = rnd.Next(1, 7);
List<GeoJSON.Net.Feature.Feature> featureList = new List<GeoJSON.Net.Feature.Feature>();
for (int i = 1; i <= dice; i++)
{
thisRndLocation = getLocation(lat, lon, meters);
GeoJSON.Net.Geometry.GeographicPosition latlon = new GeoJSON.Net.Geometry.GeographicPosition(thisRndLocation.lat, thisRndLocation.lon, 0);
GeoJSON.Net.Geometry.Point point = new GeoJSON.Net.Geometry.Point(latlon);
Dictionary<string, object> properties = new Dictionary<string, object>();
properties.Add("Color", "Blue");
GeoJSON.Net.Feature.Feature feature = new GeoJSON.Net.Feature.Feature(point, properties);
feature.Id = "FeatureId: " + i;
featureList.Add(feature);
//var coOrdinates = new GeoJSON.Net.Geometry.GeographicPosition(thisRndLocation.lat, thisRndLocation.lon);
//Positions.Add(coOrdinates);
//locationsList.Add(thisRndLocation);
//var x = locationsList;
}
return featureList;
}
在提琴手中我得到了错误,这是我的Ajax
$.ajax({
type: 'Post',
contentType: "application/json; charset=utf-8",
url: 'http://localhost:8506/api/' + 'Geo' + '/?alloworigin=true',
data: JSON.stringify({ lat: lat, lon: lon }),
dataType: 'json',
success: function (data) {
for (var i = 0; i < data.length; i++) {
var obj = data[i];
console.log(obj.id);
}
},
error: function (msg) {
alert(msg.responsetext);
}
});
我尝试过的东西将newtonsoft升级到版本6
将此行添加到application_start
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
在WebApi配置的Register方法中覆盖:
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
到目前为止没有任何好处,任何想法? , 谢谢
这是固定类,确保您使用正确的基础GeoJSon
namespace GeoJSON.Net.Geometry
{
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
/// <summary>
/// In geography, a point refers to a Position on a map, expressed in latitude and longitude.
/// </summary>
/// <seealso cref="http://geojson.org/geojson-spec.html#point"/>
public class Point : GeoJSONObject, IGeometryObject
{
/// <summary>
/// Initializes a new instance of the <see cref="Point"/> class.
/// </summary>
/// <param name="coordinates">The Position.</param>
public Point(GeoJSON.Net.Geometry.GeographicPosition coordinates)
{
if (coordinates == null)
{
throw new ArgumentNullException("coordinates");
}
this.Coordinates = new List<GeoJSON.Net.Geometry.GeographicPosition> { coordinates };
this.Type = GeoJSONObjectType.Point;
}
/// <summary>
/// Initializes a new instance of the <see cref="Point"/> class.
/// </summary>
/// <param name="coordinates">The Position.</param>
public Point(List<GeoJSON.Net.Geometry.GeographicPosition> coordinates)
{
if (coordinates == null)
{
throw new ArgumentNullException("coordinates");
}
this.Coordinates = coordinates;
this.Type = GeoJSONObjectType.Point;
}
/// <summary>
/// Gets the Coordinate(s).
/// </summary>
/// <value>The Coordinates.</value>
[JsonProperty(PropertyName = "coordinates", Required = Required.Always)]
//[JsonConverter(typeof(PositionConverter))]
public List<GeoJSON.Net.Geometry.GeographicPosition> Coordinates { get; private set; }
}
}
1 回答
在我看来,GeoJSON的库还没有实现write方法 . 如果我们在github上跳转到source for that file,那么我们可以看到它实际上还没有实现 . 然而,该项目的一些分支似乎确实有实施 . 您可以尝试下载mapbutcher's fork的代码并构建它 .