発生している問題
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#
1public static string GetVideoTitle() 2{ 3 string videoId = "IGInsosP0Ac" 4 string api = $"https://www.googleapis.com/youtube/v3/videos?part=snippet&id={videoId}&key={googleApiKey}"; 5 var json = new WebClient().DownloadString(api); 6 var ytd = JsonConvert.DeserializeObject<YouTubeDataAPIv3SnippetData>(json); 7 string result = ytd.items[0].snippet.title; 8 return result; 9}
YouTubeDataAPIv3SnippetData.cs (一部省略)
c#
1class YouTubeDataAPIv3SnippetData 2{ 3 public class PageInfo 4 { 5 public int totalResults { get; set; } 6 public int resultsPerPage { get; set; } 7 } 8 9 public class Default 10 { 11 public string url { get; set; } 12 public int width { get; set; } 13 public int height { get; set; } 14 } 15 16 public class Medium 17 { 18 public string url { get; set; } 19 public int width { get; set; } 20 public int height { get; set; } 21 } 22 23 public class High 24 { 25 public string url { get; set; } 26 public int width { get; set; } 27 public int height { get; set; } 28 } 29 30 public class Standard 31 { 32 public string url { get; set; } 33 public int width { get; set; } 34 public int height { get; set; } 35 } 36 37 public class Maxres 38 { 39 public string url { get; set; } 40 public int width { get; set; } 41 public int height { get; set; } 42 } 43 44 public class Thumbnails 45 { 46 public Default @default { get; set; } 47 public Medium medium { get; set; } 48 public High high { get; set; } 49 public Standard standard { get; set; } 50 public Maxres maxres { get; set; } 51 } 52 53 public class Localized 54 { 55 public string title { get; set; } 56 public string description { get; set; } 57 } 58 59 public class Snippet 60 { 61 public DateTime publishedAt { get; set; } 62 public string channelId { get; set; } 63 public string title { get; set; } 64 public string description { get; set; } 65 public Thumbnails thumbnails { get; set; } 66 public string channelTitle { get; set; } 67 public List<string> tags { get; set; } 68 public string categoryId { get; set; } 69 public string liveBroadcastContent { get; set; } 70 public Localized localized { get; set; } 71 } 72 73 public class Item 74 { 75 public string kind { get; set; } 76 public string etag { get; set; } 77 public string id { get; set; } 78 public Snippet snippet { get; set; } 79 } 80 81 public string kind { get; set; } 82 public string etag { get; set; } 83 public PageInfo pageInfo { get; set; } 84 public List<Item> items { get; set; } 85}
YouTubeDataAPIv3から返されるJson
json
1{ 2 "kind": "youtube#videoListResponse", 3 "etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/jwhb_FJuvFIgM29Nxg3M12Z67eM\"", 4 "pageInfo": { 5 "totalResults": 1, 6 "resultsPerPage": 1 7 }, 8 "items": [ 9 { 10 "kind": "youtube#video", 11 "etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/CiszAW_8Yo76GYB0bpHxx6-vvf0\"", 12 "id": "IGInsosP0Ac", 13 "snippet": { 14 "publishedAt": "2018-11-23T04:55:15.000Z", 15 "channelId": "UCzycs8MqvIY4nXWwS-v4J9g", 16 "title": "ONE OK ROCK: Stand Out Fit In [OFFICIAL VIDEO]", 17 "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", 18 "thumbnails": { 19 "default": { 20 "url": "https://i.ytimg.com/vi/IGInsosP0Ac/default.jpg", 21 "width": 120, 22 "height": 90 23 }, 24 "medium": { 25 "url": "https://i.ytimg.com/vi/IGInsosP0Ac/mqdefault.jpg", 26 "width": 320, 27 "height": 180 28 }, 29 "high": { 30 "url": "https://i.ytimg.com/vi/IGInsosP0Ac/hqdefault.jpg", 31 "width": 480, 32 "height": 360 33 }, 34 "standard": { 35 "url": "https://i.ytimg.com/vi/IGInsosP0Ac/sddefault.jpg", 36 "width": 640, 37 "height": 480 38 }, 39 "maxres": { 40 "url": "https://i.ytimg.com/vi/IGInsosP0Ac/maxresdefault.jpg", 41 "width": 1280, 42 "height": 720 43 } 44 }, 45 "channelTitle": "ONE OK ROCK", 46 "tags": [ 47 "ONE OK ROCK", 48 "ワンオク", 49 "ワンオクロック", 50 "Taka", 51 "Toru", 52 "Ryota", 53 "Tomoya", 54 "バンド", 55 "OOR", 56 "ロック" 57 ], 58 "categoryId": "10", 59 "liveBroadcastContent": "none", 60 "localized": { 61 "title": "ONE OK ROCK: Stand Out Fit In [OFFICIAL VIDEO]", 62 "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" 63 } 64 } 65 } 66 ] 67}
###試したこと・気付いたこと
動画のIDを変えるとできました。一部の動画で例外が発生するようです。
[追伸] 色々試したところ"tags"
に日本語が用いられているJsonファイルだとこのエラーが発生するようです。
###環境
- Framework4.7.2
- Windows10 Pro 64bit ver.1809 build.17763.615
- Newtonsoft.Json ver.12.0.0.0
回答1件
あなたの回答
tips
プレビュー