【问题标题】:Can't deserialize JSON to enum无法将 JSON 反序列化为枚举
【发布时间】:2018-09-30 08:53:46
【问题描述】:

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

为什么它不起作用?

【问题讨论】:

  • 您可以发布您的数据模型 (CoubBig) 吗?
  • @Neil 你在这里。
  • 无法复制。在注释掉未定义类型的属性后,我尝试使用CoubBig 反序列化coub.com/api/v2/coubs/4951721 处的JSON,并且Type 被成功反序列化。我确实找到了两个需要设置为空的属性:public bool? Cotd { get; set; }public int? RecoubTo { get; set; }。你使用的是什么版本的 Json.NET?

标签: c# json enums json.net deserialization


【解决方案1】:

您可以从下面的代码中获得帮助,将类转换为 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 }

【讨论】:

  • 没那么简单。例如,在 JSON 中我得到这个:Coub::Simple 不匹配 Simple1。而且似乎属性也没有改变。
【解决方案2】:

我找到了问题的根源。
当我尝试使用EnumMember 属性时,我无法(因为System.Runtime.Serialization 没有添加到项目中),所以我搜索了它的源代码并将其插入到项目中。当我仔细查看时,我注意到System.Runtime.Serialization 没有添加到项目中。我删除了我插入的属性并将System.Runtime.Serialization 添加到项目中。然后就成功了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-04
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多