反序列化 JSON 时转换属性

问题描述

从 HTTP GET 请求中,我得到以下字符串结果:

{
  "id": "5963363","private": "0","created_at": "2021-06-02 16:15:40","release_date": "2021-06-02 16:15:40","release_timestamp": 1622643340,"user_id": "10037997","duration": "16","permalink": "the-shortest-interview-of-magnus-carlsen-only-10-seconds","description": "","geo": "","tags": "","taged_artists": "","bpm": "92.5","key": "Bm","license": "","version": "","type": "","downloadable": "1","genre": "Other","genre_slush": "other","title": "The Shortest Interview of Magnus Carlsen - ONLY 10 SECONDS!","uri": "https://api-v2.hearthis.at/sergiu-molnar/the-shortest-interview-of-magnus-carlsen-only-10-seconds/","permalink_url": "https://hearthis.at/sergiu-molnar/the-shortest-interview-of-magnus-carlsen-only-10-seconds/","thumb": "https://images.hearthis.at/1/_/2/_/uploads/dummy/w200_h200_q70_----1_2021_04-2.jpg","artwork_url": "https://images.hearthis.at/1/_/2/_/uploads/dummy/w500_q70_----1_2021_04-2.jpg","artwork_url_retina": "https://images.hearthis.at/1/_/2/_/uploads/dummy/w1000_q70_----1_2021_04-2.jpg","background_url": "https://images.hearthis.at/1/_/2/_/uploads/dummy/w565_h565_c000000_q70_----1_2021_04-2.jpg","waveform_data": "https://cdn.hearthis.at/_/wave_data/10037997/3000_6ab732ed56cc403f09c0dc9f86f80342.mp3_1622643340.js","waveform_url": "https://cdn.hearthis.at/_/cache/waveform_mask/5/9/5963363.png","user": {
    "id": "10037997","permalink": "sergiu-molnar","username": "Sergiu Molnar","caption": "","uri": "https://api-v2.hearthis.at/sergiu-molnar/","permalink_url": "https://hearthis.at/sergiu-molnar/","avatar_url": "https://images.hearthis.at/1/_/2/_/uploads/dummy/w512_q70_----1_2021_08-2.jpg"
  },"stream_url": "https://hearthis.app/sergiu-molnar/the-shortest-interview-of-magnus-carlsen-only-10-seconds/listen/?s=vCc","preview_url": "https://preview.hearthis.at/files/6ab732ed56cc403f09c0dc9f86f80342.mp3","download_url": "https://hearthis.app/sergiu-molnar/the-shortest-interview-of-magnus-carlsen-only-10-seconds/download/","download_filename": "The Shortest Interview of Magnus Carlsen - ONLY 10 SECONDS! (hearthis.at).mp3","playback_count": "1","download_count": "0","favoritings_count": "0","reshares_count": "0","comment_count": "0","played": false,"favorited": false,"liked": false,"reshared": false
}

如您所见,几乎所有属性都是字符串,即使这些属性应该是 intdate

要做的是反序列化对象并转换属性。 为此,我使用 Newtonsoft.Json 如下:

JsonConvert.DeserializeObject<TrackDetails>(response);

这是 TrackDetails 类:

public class TrackDetails
{
     [JsonProperty("id")]
     public string Id { get; set; }

     ...
}

因为在结果中,Id一个字符串,我希望在阅读这些文章后将其设为 int:

我尝试创建一个像这样的自定义转换器:

public class StringToIntConverter : JsonConverter
{
     public override bool CanConvert(Type objectType)
     {
          return (objectType == typeof(string));
     }

     public override object ReadJson(JsonReader reader,Type objectType,object existingValue,JsonSerializer serializer)
     {
          return int.Parse(reader.Value.ToString());
     }

     public override void WriteJson(JsonWriter writer,object value,JsonSerializer serializer)
     {
          writer.WriteValue(int.Parse((string)value));
     }
}

这是这样应用的:

[JsonProperty("id")]
[JsonConverter(typeof(StringToIntConverter))]
public string Id { get; set; }

但我收到以下错误

Newtonsoft.Json.JsonSerializationException
  Message=Error setting value to 'Id' on 'API.Models.TrackDetails'.

Inner Exception 1:
  InvalidCastException: Unable to cast object of type 'system.int32' to type 'System.String'.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)