簡體   English   中英

解析JSON.net中的枚舉

[英]parsing an enumeration in JSON.net

我正在使用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錯了嗎?


編輯:我將最新的JSON.NET移植到MT,我仍然得到完全相同的錯誤。

根據JSON.NET文檔,默認行為是對Enums使用int值: http//james.newtonking.com/projects/json/help/SerializationGuide.html

您可以通過在枚舉上添加帶有StringEnumConverter的JsonConverter屬性來更改它...

/// <summary>
/// The geometry type.
/// </summary>
[DataContract]
[JsonConverter(typeof(StringEnumConverter))]
public enum GeometryType

文檔: 使用JsonConverters進行序列化

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM