无法反序列化JSON以枚举

诺埃尔·内梅斯(Noel Nemeth)

当我尝试反序列化JSON时,总是出现错误。CoubType包含在CoubBig类。JSON的示例:http : //coub.com/api/v2/coubs/4951721

[JsonConverter(typeof(StringEnumConverter))]
public enum CoubType
{
    [EnumMember(Value = "Coub::Simple")]
    Simple = 1,
    [EnumMember(Value = "Coub::Temp")]
    Temp = 2,
    EnumMember(Value = "Coub::Recoub")]
    Recoub = 3
}

JsonConvert.DeserializeObject<CoubBig>(await httpResponse.Content.ReadAsStringAsync());

public partial class CoubBig
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("type")]
    public CoubType Type { get; set; }

    [JsonProperty("permalink")]
    public string Permalink { get; set; }

    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("visibility_type")]
    public VisibilityType VisibilityType { get; set; }

    [JsonProperty("original_visibility_type")]
    public VisibilityType OriginalVisibilityType { get; set; }

    [JsonProperty("channel_id")]
    public int ChannelId { get; set; }

    [JsonProperty("created_at")]
    public DateTimeOffset CreatedAt { get; set; }

    [JsonProperty("updated_at")]
    public DateTimeOffset UpdatedAt { get; set; }

    [JsonProperty("is_done")]
    public bool IsDone { get; set; }

    [JsonProperty("views_count")]
    public int ViewsCount { get; set; }

    [JsonProperty("cotd")]
    public bool Cotd { get; set; }

    [JsonProperty("cotd_at")]
    public object CotdAt { get; set; }

    [JsonProperty("original_sound")]
    public bool OriginalSound { get; set; }

    [JsonProperty("has_sound")]
    public bool HasSound { get; set; }

    [JsonProperty("recoub_to")]
    public int RecoubTo { get; set; }

    [JsonProperty("file_versions")]
    public FileVersions FileVersions { get; set; }

    [JsonProperty("audio_versions")]
    public AudioVersions AudioVersions { get; set; }

    [JsonProperty("image_versions")]
    public ThumbnailImageVersions ImageVersions { get; set; }

    [JsonProperty("first_frame_versions")]
    public FirstFrameVersions FirstFrameVersions { get; set; }

    [JsonProperty("dimensions")]
    public Dimensions Dimensions { get; set; }

    [JsonProperty("age_restricted")]
    public bool AgeRestricted { get; set; }

    [JsonProperty("allow_reuse")]
    public bool AllowReuse { get; set; }

    [JsonProperty("banned")]
    public bool Banned { get; set; }

    [JsonProperty("external_download")]
    public ExternalDownload ExternalDownload { get; set; }

    [JsonProperty("channel")]
    public ChannelSmall Channel { get; set; }

    [JsonProperty("percent_done")]
    public long PercentDone { get; set; }

    [JsonProperty("tags")]
    public Tag[] Tags { get; set; }

    [JsonProperty("recoubs_count")]
    public long RecoubsCount { get; set; }

    [JsonProperty("likes_count")]
    public long LikesCount { get; set; }

    [JsonProperty("raw_video_id")]
    public long RawVideoId { get; set; }

    [JsonProperty("media_blocks")]
    public MediaBlocks MediaBlocks { get; set; }

    [JsonProperty("raw_video_thumbnail_url")]
    public Uri RawVideoThumbnailUrl { get; set; }

    [JsonProperty("raw_video_title")]
    public string RawVideoTitle { get; set; }


    [JsonProperty("video_block_banned")]
    public bool VideoBlockBanned { get; set; }

    [JsonProperty("duration")]
    public float Duration { get; set; }
}

例外

Newtonsoft.Json.JsonSerializationException:'将值“ Coub :: Simple”转换为'Coub.Net.Objects.CoubType'时出错。路径“类型”,第1行,位置162。”
内部异常
ArgumentException:找不到请求的值'Coub :: Simple'。

为什么不起作用?

诺埃尔·内梅斯(Noel Nemeth)

我找到了问题根源。
当我尝试使用该EnumMember属性时,由于System.Runtime.Serialization没有将其添加到项目中,因此无法(因为未将其添加到项目中),因此我搜索了源代码并将其插入到项目中。当我看起来更好时,我注意到System.Runtime.Serialization该项目没有添加。我删除了插入并添加System.Runtime.Serialization到项目中的属性然后它起作用了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章