発生している問題
Newtonsoft.JsonConvert.DeserializeObject
でYouTubeDataAPIv3から返されるJsonをデシリアライズしようとすると、予期しない文字が発見されデシリアライズできない。
エラーメッセージ
Newtonsoft.Json.JsonReaderException: 'After parsing a value an unexpected character was encountered: O. Path 'items[0].snippet.tags[7]', line 55, position 6.'
該当のソースコード
MainWindow.xaml.cs (一部省略)
c#
public static string GetVideoTitle() { string videoId = "IGInsosP0Ac" string api = $"https://www.googleapis.com/youtube/v3/videos?part=snippet&id={videoId}&key={googleApiKey}"; var json = new WebClient().DownloadString(api); var ytd = JsonConvert.DeserializeObject<YouTubeDataAPIv3SnippetData>(json); string result = ytd.items[0].snippet.title; return result; }
YouTubeDataAPIv3SnippetData.cs (一部省略)
c#
class YouTubeDataAPIv3SnippetData { public class PageInfo { public int totalResults { get; set; } public int resultsPerPage { get; set; } } public class Default { public string url { get; set; } public int width { get; set; } public int height { get; set; } } public class Medium { public string url { get; set; } public int width { get; set; } public int height { get; set; } } public class High { public string url { get; set; } public int width { get; set; } public int height { get; set; } } public class Standard { public string url { get; set; } public int width { get; set; } public int height { get; set; } } public class Maxres { public string url { get; set; } public int width { get; set; } public int height { get; set; } } public class Thumbnails { public Default @default { get; set; } public Medium medium { get; set; } public High high { get; set; } public Standard standard { get; set; } public Maxres maxres { get; set; } } public class Localized { public string title { get; set; } public string description { get; set; } } public class Snippet { public DateTime publishedAt { get; set; } public string channelId { get; set; } public string title { get; set; } public string description { get; set; } public Thumbnails thumbnails { get; set; } public string channelTitle { get; set; } public List<string> tags { get; set; } public string categoryId { get; set; } public string liveBroadcastContent { get; set; } public Localized localized { get; set; } } public class Item { public string kind { get; set; } public string etag { get; set; } public string id { get; set; } public Snippet snippet { get; set; } } public string kind { get; set; } public string etag { get; set; } public PageInfo pageInfo { get; set; } public List<Item> items { get; set; } }
YouTubeDataAPIv3から返されるJson
json
{ "kind": "youtube#videoListResponse", "etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/jwhb_FJuvFIgM29Nxg3M12Z67eM\"", "pageInfo": { "totalResults": 1, "resultsPerPage": 1 }, "items": [ { "kind": "youtube#video", "etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/CiszAW_8Yo76GYB0bpHxx6-vvf0\"", "id": "IGInsosP0Ac", "snippet": { "publishedAt": "2018-11-23T04:55:15.000Z", "channelId": "UCzycs8MqvIY4nXWwS-v4J9g", "title": "ONE OK ROCK: Stand Out Fit In [OFFICIAL VIDEO]", "description": "ONE OK ROCK's official video for 'Stand Out Fit In’. \nDirected By Peter Huang\n\nSubtitles by Taka and Yasuko Ishida\n\nPre-order Eye Of The Storm today: https://amuse-inc.lnk.to/ONEOKROCK\n\nFind ONE OK ROCK on… \nSite: http://www.oneokrock.com/ \nFacebook: http://smarturl.it/OORfacebook \nTwitter: http://smarturl.it/OORtwitter \nInstagram: http://smarturl.it/OORinstagram \nYouTube: http://smarturl.it/OORyoutube \nSpotify: http://smarturl.it/OORspotify", "thumbnails": { "default": { "url": "https://i.ytimg.com/vi/IGInsosP0Ac/default.jpg", "width": 120, "height": 90 }, "medium": { "url": "https://i.ytimg.com/vi/IGInsosP0Ac/mqdefault.jpg", "width": 320, "height": 180 }, "high": { "url": "https://i.ytimg.com/vi/IGInsosP0Ac/hqdefault.jpg", "width": 480, "height": 360 }, "standard": { "url": "https://i.ytimg.com/vi/IGInsosP0Ac/sddefault.jpg", "width": 640, "height": 480 }, "maxres": { "url": "https://i.ytimg.com/vi/IGInsosP0Ac/maxresdefault.jpg", "width": 1280, "height": 720 } }, "channelTitle": "ONE OK ROCK", "tags": [ "ONE OK ROCK", "ワンオク", "ワンオクロック", "Taka", "Toru", "Ryota", "Tomoya", "バンド", "OOR", "ロック" ], "categoryId": "10", "liveBroadcastContent": "none", "localized": { "title": "ONE OK ROCK: Stand Out Fit In [OFFICIAL VIDEO]", "description": "ONE OK ROCK's official video for 'Stand Out Fit In’. \nDirected By Peter Huang\n\nSubtitles by Taka and Yasuko Ishida\n\nPre-order Eye Of The Storm today: https://amuse-inc.lnk.to/ONEOKROCK\n\nFind ONE OK ROCK on… \nSite: http://www.oneokrock.com/ \nFacebook: http://smarturl.it/OORfacebook \nTwitter: http://smarturl.it/OORtwitter \nInstagram: http://smarturl.it/OORinstagram \nYouTube: http://smarturl.it/OORyoutube \nSpotify: http://smarturl.it/OORspotify" } } } ] }
###試したこと・気付いたこと
動画のIDを変えるとできました。一部の動画で例外が発生するようです。
[追伸] 色々試したところ"tags"
に日本語が用いられているJsonファイルだとこのエラーが発生するようです。
###環境
- Framework4.7.2
- Windows10 Pro 64bit ver.1809 build.17763.615
- Newtonsoft.Json ver.12.0.0.0
まだ回答がついていません
会員登録して回答してみよう