【发布时间】: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