解析在JSON.net枚举 [英] parsing an enumeration in JSON.net
问题描述
我使用JSON.net(也许v3.5ish?从华侨城,2010年)。我试图反序列化一些JSON到一个枚举:
i'm using JSON.net (maybe v3.5ish? it's from oct. 2010). and i'm trying to deserialize some json into an enumeration:
geometryType:esriGeometryPolygon
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。
but it throws an error saying "Error converting value "esriGeometryPolygon" to type '...GeometryType'.
我缺少的是在这里吗?
是它,因为它是一个老版本(我使用MonoTouch的端口:的https:/这已不是一年更新)?还是我让我的错datacontract /github.com/chrisntr/Newtonsoft.Json?
is it because it's an old version (i'm using the monotouch port: https://github.com/chrisntr/Newtonsoft.Json which hasn't been updated in a year)? or did i get my datacontract wrong?
编辑:我移植的最新JSON.NET到MT,我仍然得到确切的同样的错误。
i ported the latest JSON.NET to MT and i'm still getting the exact same error.
推荐答案
据JSON.NET文档,默认行为是使用枚举int值:的 http://james.newtonking.com/projects/json/help/SerializationGuide.html
According to JSON.NET documentation, default behavior is to use int value for Enums : http://james.newtonking.com/projects/json/help/SerializationGuide.html
您可以通过添加JsonConverter改变你用枚举属性StringEnumConverter ...
You can change that by adding a JsonConverter attribute with StringEnumConverter on your enum...
/// <summary>
/// The geometry type.
/// </summary>
[DataContract]
[JsonConverter(typeof(StringEnumConverter))]
public enum GeometryType
这篇关于解析在JSON.net枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!