无法反序列化JSON以枚举

时间:2018-09-30 08:53:47

标签: c# json enums json.net deserialization

当我尝试反序列化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'。

为什么不起作用?

2 个答案:

答案 0 :(得分:2)

您可以从下面的代码获得帮助,以将类转换为json

public enum Role
{
    Admin,
    Guest
}
public class User
{
    public string Name { get; set; }
    public bool Active { get; set; }
    public DateTime CreatedDate { get; set; }
    public Role Roles { get; set; }
}

通过

转换json
public User ConvertJson(string json)
{
    var obj = JsonConvert.DeserializeObject<User>(json);
    return obj;
}

两种模式json

{  'Name': 'Your Name', 'Active': true, 'CreatedDate': '2013-01-20T00:00:00Z','Roles': 'Admin'}


{  'Name': 'Your Name', 'Active': true, 'CreatedDate': '2013-01-20T00:00:00Z','Roles': 1 }

答案 1 :(得分:0)

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