前提・実現したいこと
GoogleブックスAPIを使ってISBNコードから書籍を検索するアプリを制作しているのですが、「The data couldn’t be read because it isn’t in the correct format.」
とエラーが表示されてしまいます。
構造体の書き方が間違えているのかなと思ったのですが、下記のレスポンスをマッピングする場合の構造体の記述方法は自分のやり方で合っていますでしょうか?
レスポンス
json
1{ 2 "kind": "books#volumes", 3 "totalItems": 1, 4 "items": [ 5 { 6 "kind": "books#volume", 7 "id": "s4CRHAAACAAJ", 8 "etag": "deolUbOxJwI", 9 "selfLink": "https://www.googleapis.com/books/v1/volumes/s4CRHAAACAAJ", 10 "volumeInfo": { 11 "title": "アラビアの夜の種族", 12 "authors": [ 13 "古川日出男" 14 ], 15 "publishedDate": "2006-07", 16 "description": "聖遷暦1213年。偽りの平穏に満ちたエジプト。迫り来るナポレオン艦隊、侵掠の凶兆に、迎え撃つ支配階級奴隷アイユーブの秘策はただひとつ、極上の献上品。それは読む者を破滅に導き、歴史を覆す書物、『災厄の書』―。アイユーブの術計は周到に準備される。権力者を眩惑し滅ぼす奔放な空想。物語は夜、密かにカイロの片隅で譚り書き綴られる。「妖術師アーダムはほんとうに醜い男でございました...」。驚異の物語、第一部。", 17 "industryIdentifiers": [ 18 { 19 "type": "ISBN_10", 20 "identifier": "4043636032" 21 }, 22 { 23 "type": "ISBN_13", 24 "identifier": "9784043636037" 25 } 26 ], 27 "readingModes": { 28 "text": false, 29 "image": false 30 }, 31 "pageCount": 277, 32 "printType": "BOOK", 33 "categories": [ 34 "Detective and mystery stories" 35 ], 36 "maturityRating": "NOT_MATURE", 37 "allowAnonLogging": false, 38 "contentVersion": "preview-1.0.0", 39 "imageLinks": { 40 "smallThumbnail": "http://books.google.com/books/content?id=s4CRHAAACAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api", 41 "thumbnail": "http://books.google.com/books/content?id=s4CRHAAACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api" 42 }, 43 "language": "ja", 44 "previewLink": "http://books.google.co.jp/books?id=s4CRHAAACAAJ&dq=isbn:9784043636037&hl=&cd=1&source=gbs_api", 45 "infoLink": "http://books.google.co.jp/books?id=s4CRHAAACAAJ&dq=isbn:9784043636037&hl=&source=gbs_api", 46 "canonicalVolumeLink": "https://books.google.com/books/about/%E3%82%A2%E3%83%A9%E3%83%93%E3%82%A2%E3%81%AE%E5%A4%9C%E3%81%AE%E7%A8%AE%E6%97%8F.html?hl=&id=s4CRHAAACAAJ" 47 }, 48 "saleInfo": { 49 "country": "JP", 50 "saleability": "NOT_FOR_SALE", 51 "isEbook": false 52 }, 53 "accessInfo": { 54 "country": "JP", 55 "viewability": "NO_PAGES", 56 "embeddable": false, 57 "publicDomain": false, 58 "textToSpeechPermission": "ALLOWED", 59 "epub": { 60 "isAvailable": false 61 }, 62 "pdf": { 63 "isAvailable": false 64 }, 65 "webReaderLink": "http://play.google.com/books/reader?id=s4CRHAAACAAJ&hl=&printsec=frontcover&source=gbs_api", 66 "accessViewStatus": "NONE", 67 "quoteSharingAllowed": false 68 }, 69 "searchInfo": { 70 "textSnippet": "聖遷暦1213年。偽りの平穏に満ちたエジプト。迫り来るナポレオン艦隊、侵掠の凶兆に、迎え撃つ支配階級奴隷アイユーブの秘策はただひとつ、極上の献上品。それは読む者を破 ..." 71 } 72 } 73 ] 74} 75
構造体(Codable)
swift
1 struct Article: Codable { 2 var kind: String 3 var totalItems: String 4 var items: [items] 5 struct items: Codable { 6 var kind: String 7 var id: String 8 var etag: String 9 var selfLink: String 10 struct volumeInfo: Codable { 11 var title: String 12 var authors: [String] 13 var publishedDate: Date 14 var description: String 15 var industryIdentifiers: [industryIdentifiers] 16 struct industryIdentifiers: Codable { 17 var type: String 18 var identifier: String 19 } 20 struct readingModes: Codable { 21 var text: Bool 22 var image: Bool 23 } 24 var pageCount: Int 25 var printType: String 26 var categories: [String] 27 var maturityRating: String 28 var allowAnonLogging: Bool 29 var contentVersion: String 30 struct imageLinks: Codable { 31 var smallThumbnail: String 32 var thumbnail: String 33 } 34 var language: String 35 var previewLink: String 36 var infoLink: String 37 var canonicalVolumeLink: String 38 } 39 struct saleInfo: Codable { 40 var country: String 41 var saleability: String 42 var isEbook: Bool 43 } 44 struct accessInfo: Codable { 45 var country: String 46 var viewability: String 47 var embeddable: Bool 48 var publicDomain: Bool 49 var textToSpeechPermission: String 50 struct epub: Codable { 51 var isAvailable: Bool 52 } 53 struct pdf: Codable { 54 var isAvailable: Bool 55 } 56 var webReaderLink: String 57 var accessViewStatus: String 58 var quoteSharingAllowed: Bool 59 } 60 struct searchInfo: Codable { 61 var textSnippet: String 62 } 63 } 64 }
実行コード
swift
1 let url = "https://www.googleapis.com/books/v1/volumes" 2 3 4 guard var urlComponents = URLComponents(string: url) else { 5 return 6 } 7 // https://www.googleapis.com/books/v1/volumes?q=isbn:9784043636037 8 urlComponents.queryItems = [ 9 URLQueryItem(name: "q", value: "isbn:9784043636037"), 10 ] 11 12 let task = URLSession.shared.dataTask(with: urlComponents.url!) { data, response, error in 13 14 guard let jsonData = data else { 15 return 16 } 17 print(jsonData) 18 do { 19 let articles = try JSONDecoder().decode([Article].self, from: jsonData) 20 print(articles) 21 } catch { 22 print(error.localizedDescription) 23 } 24 } 25 task.resume()
補足情報
https://qiita.com/abouch/items/aca979b71ad792478687
上記のページを参考にさせて頂いてます。
構造体とurlを参考ページのものに合わせると実行できました。
回答2件
あなたの回答
tips
プレビュー