前提・実現したいこと
SwifのCodableを使ってFlickrAPIのレスポンス(JSON)を読み込みたい
発生している問題・エラーメッセージ
typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "photos", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))
該当のソースコード
swift
1//: Playground - noun: a place where people can play 2 3import UIKit 4import Foundation 5 6struct Photo: Codable { 7 let id: String 8 let owner: String 9 let secret: String 10 let server: String 11 let farm: Int 12 let title: String 13 let ispublic: Int 14 let isfriend: Int 15 let isfamily: Int 16 let url_z: String 17 let height_z: String 18 let width_z: String 19} 20 21struct Photos: Codable { 22 let page: Int 23 let pages: Int 24 let perpage: Int 25 let total: String 26 let photo: [Photo] 27} 28 29struct FlickrJson: Codable { 30 let photos: [Photos] 31 let stat: String 32} 33 34let url: URL = URL(string: "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=b92e39e1e3032c52368b9dde00a366b6&text=apple&sort=interestingness-desc&extras=url_z&per_page=5&format=json&nojsoncallback=1&api_sig=c1373de25250f8592ce893d622ef99e8")! 35let task: URLSessionTask = URLSession.shared.dataTask(with: url, completionHandler: {data, response, error in 36 do { 37 let jsonData = try JSONDecoder().decode(FlickrJson.self, from: data!) 38 } 39 catch { 40 print(error) 41 } 42}) 43task.resume() 44
JSON
1{"photos":{"page":1,"pages":454311,"perpage":5,"total":"2271555","photo":[{"id":"13823027375","owner":"27263572@N05","secret":"2faf0ff3e1","server":"2932","farm":3,"title":"apple trees","ispublic":1,"isfriend":0,"isfamily":0,"url_z":"https://farm3.staticflickr.com/2932/13823027375_2faf0ff3e1_z.jpg","height_z":"427","width_z":"640"},{"id":"14923643380","owner":"56393389@N05","secret":"16107e4234","server":"3862","farm":4,"title":"Apple time","ispublic":1,"isfriend":0,"isfamily":0,"url_z":"https://farm4.staticflickr.com/3862/14923643380_16107e4234_z.jpg","height_z":"499","width_z":"632"},{"id":"3005896202","owner":"11142308@N07","secret":"95e2e72bd2","server":"3152","farm":4,"title":"Apple... Greek Moonshine","ispublic":1,"isfriend":0,"isfamily":0,"url_z":"https://farm4.staticflickr.com/3152/3005896202_95e2e72bd2_z.jpg","height_z":398,"width_z":"640"},{"id":"15210936448","owner":"65068817@N05","secret":"b26fb882b1","server":"3907","farm":4,"title":"Apples...","ispublic":1,"isfriend":0,"isfamily":0,"url_z":"https://farm4.staticflickr.com/3907/15210936448_b26fb882b1_z.jpg","height_z":"425","width_z":"640"},{"id":"43537647531","owner":"128091419@N08","secret":"b43be4df8d","server":"849","farm":1,"title":"Apple rain","ispublic":1,"isfriend":0,"isfamily":0,"url_z":"https://farm1.staticflickr.com/849/43537647531_b43be4df8d_z.jpg","height_z":"640","width_z":"480"}]},"stat":"ok"}
試したこと
エラーメッセージに"Expected to decode Array<Any> but found a dictionary instead."とあるのでCodableの書き方が違うのかと思って見直したのですが解決できませんでした。
補足情報(FW/ツールのバージョンなど)
Xcode10.0, Swift4, Codable, playground

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。