我正在使用JSON.net(也许是v3.5ish?它来自2010年10月)。我试图将一些json反序列化为枚举:
geometryType:“esriGeometryPolygon”
我有这个枚举:
/// <summary>
/// The geometry type.
/// </summary>
[DataContract]
public enum GeometryType
{
/// <summary>
/// Refers to geometry type Envelope
/// </summary>
[EnumMember(Value = "esriGeometryEnvelope")]
Envelope,
/// <summary>
/// Refers to geometry type MultiPoint
/// </summary>
[EnumMember(Value = "esriGeometryMultipoint")]
MultiPoint,
/// <summary>
/// Refers to geometry type MapPoint
/// </summary>
[EnumMember(Value = "esriGeometryPoint")]
Point,
/// <summary>
/// Refers to geometry type Polygon
/// </summary>
[EnumMember(Value = "esriGeometryPolygon")]
Polygon,
/// <summary>
/// Refers to geometry type Polyline
/// </summary>
[EnumMember(Value = "esriGeometryPolyline")]
Polyline
}
但它会抛出一个错误,说“将值转换错误”esriGeometryPolygon“输入'... GeometryType'。
我在这里缺少什么?
是因为它是一个旧版本(我使用的是monotouch端口:https://github.com/chrisntr/Newtonsoft.Json,它在一年内没有更新)?或者我的datacontract错了吗?
答案 0 :(得分:38)
根据JSON.NET文档,默认行为是对枚举使用int值:http://james.newtonking.com/projects/json/help/SerializationGuide.html
您可以通过在枚举上添加带有StringEnumConverter的JsonConverter属性来更改它...
/// <summary>
/// The geometry type.
/// </summary>
[DataContract]
[JsonConverter(typeof(StringEnumConverter))]
public enum GeometryType