前提・実現したいこと
現在SwiftでぐるなびAPIの応援口コミのデータを取得しようとしています。
該当ソースコード
・JsonStruct
・JSONデータ
発生している問題・エラーメッセージ
Jsonデータの取得はできておりますが、JSONStructの『case review = "0"』 の部分の数字(文字列)を動的に変更する方法がわからない状況です。 現在の挙動としては、『case review = "0"』としているため、データ件数が複数件あったとしても Jsonデータの1つ目しか取得できないです。
該当のソースコード
JSONStruct
1class JsonReviewsList { 2 // MARK: - Reviews 3 struct Reviews: Codable { 4 let response: Response 5 } 6 7 // MARK: - Response 8 struct Response: Codable { 9 let review: Review 10 let attributes: Attributes 11 let totalHitCount, hitPerPage: Int 12 13 enum CodingKeys: String, CodingKey { 14 15 case review = "0" ※※※※※※※※※※※※※※※※※※※※動的に変化させたい(For文?) 16 case attributes = "@attributes" 17 case totalHitCount = "total_hit_count" 18 case hitPerPage = "hit_per_page" 19 } 20 } 21 22 // MARK: - Attributes 23 struct Attributes: Codable { 24 let apiVersion: String 25 26 enum CodingKeys: String, CodingKey { 27 case apiVersion = "api_version" 28 } 29 } 30 31 // MARK: - Review 32 struct Review: Codable { 33 let photo: Photo 34 } 35 36 // MARK: - Photo 37 struct Photo: Codable { 38 let voteID, photoGenreID, photoGenreName, photoSceneID: String 39 let photoSceneName, nickname, shopID, shopName: String 40 let shopURL: String 41 let prefname: String 42 let menuID: Int 43 let menuName: String 44 let menuFinishFlag: Int 45 let areanameL, areanameM, areanameS: String 46 let imageURL: ImageURL 47 let comment, totalScore, category, latitude: String 48 let longitude: String 49 let umasoCount: Int 50 let updateDate: Date 51 let messages: Messages 52 53 enum CodingKeys: String, CodingKey { 54 case voteID = "vote_id" 55 case photoGenreID = "photo_genre_id" 56 case photoGenreName = "photo_genre_name" 57 case photoSceneID = "photo_scene_id" 58 case photoSceneName = "photo_scene_name" 59 case nickname 60 case shopID = "shop_id" 61 case shopName = "shop_name" 62 case shopURL = "shop_url" 63 case prefname 64 case menuID = "menu_id" 65 case menuName = "menu_name" 66 case menuFinishFlag = "menu_finish_flag" 67 case areanameL = "areaname_l" 68 case areanameM = "areaname_m" 69 case areanameS = "areaname_s" 70 case imageURL = "image_url" 71 case comment 72 case totalScore = "total_score" 73 case category, latitude, longitude 74 case umasoCount = "umaso_count" 75 case updateDate = "update_date" 76 case messages 77 } 78 } 79 80 // MARK: - ImageURL 81 struct ImageURL: Codable { 82 let url1024, url320, url250, url200: String 83 84 enum CodingKeys: String, CodingKey { 85 case url1024 = "url_1024" 86 case url320 = "url_320" 87 case url250 = "url_250" 88 case url200 = "url_200" 89 } 90 } 91 92 // MARK: - Messages 93 struct Messages: Codable { 94 let userMessageCount, shopMessageCount: Int 95 let firstShopMessage: FirstShopMessage 96 97 enum CodingKeys: String, CodingKey { 98 case userMessageCount = "user_message_count" 99 case shopMessageCount = "shop_message_count" 100 case firstShopMessage = "first_shop_message" 101 } 102 } 103 104 // MARK: - FirstShopMessage 105 struct FirstShopMessage: Codable { 106 let messageBody, sendDate: String 107 108 enum CodingKeys: String, CodingKey { 109 case messageBody = "message_body" 110 case sendDate = "send_date" 111 } 112 } 113 114 115}
【JSONデータ】 { "response": { "@attributes": { "api_version": "v3" }, "total_hit_count": 1389, "hit_per_page": 15, "0": { ←※※※※※※※※※※※※※※※※※※※※※※※※※※※"0"の数字が変化します。 "photo": { "vote_id": "95644", "photo_genre_id": "1", "photo_genre_name": "料理・ドリンク", "photo_scene_id": "", "photo_scene_name": "", "nickname": "ぐるなび会員", "shop_id": "6072772", "shop_name": "焼野菜 五十家", "shop_url": "https://r.gnavi.co.jp/3fjar9m50000/?ak=Y%2FdqTpL57s7y4VkTH%2BYkvUruWMirqD7LUAakY4OB0xs%3D", "prefname": "PREF26:京都府", "menu_id": 90968, "menu_name": "農園茄子と京お揚げの揚げ出し", "menu_finish_flag": 0, "areaname_l": "河原町・木屋町・先斗町", "areaname_m": "四条河原町周辺・寺町", "areaname_s": "四条河原町周辺・寺町", "image_url": { "url_1024": "https://mr.gnavi.co.jp/cont/menu_image/6f/25/95644_l.jpeg", "url_320": "https://mr.gnavi.co.jp/cont/menu_image/6f/25/95644.jpeg", "url_250": "https://mr.gnavi.co.jp/cont/menu_image/6f/25/95644_m.jpeg", "url_200": "https://mr.gnavi.co.jp/cont/menu_image/6f/25/95644_s.jpeg" }, "comment": "アツアツで美味しいです。", "total_score": "3.0", "category": "野菜料理", "distance": 0, "latitude": "", "longitude": "", "umaso_count": 0, "update_date": "2010-12-31T10:13:00+09:00", "messages": { "user_message_count": 0, "shop_message_count": 0, "first_shop_message": { "message_body": "", "send_date": "" } } } }, "1": { ←※※※※※※※※※※※※※※※※※※※※※※※※※※※ "photo": { "vote_id": "", "photo_genre_id": "", ----------- 省略 -----------
参照元クラス let url = URL(string: ”ぐるなびAPI URL”) let request = URLRequest(url: url!) let session = URLSession.shared session.dataTask(with: request){(data, response, error)in if error == nil, let data = data, ((response as? HTTPURLResponse) != nil) { //デコード let decoder: JSONDecoder = JSONDecoder() decoder.dateDecodingStrategy = .iso8601 do { //データ取得成功 let json:JsonReviewsList.Reviews = try decoder.decode(JsonReviewsList.Reviews.self, from: data) let totalHitCount:Int = Int(json.total_hit_count) let jsonreview = json.reviews for i in (0...totalHitCount-1) { print(jsonreview[i].shopID) print(jsonreview[i].shopName) } }catch {print ("error")} } }.resume()
ご教授よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/01 05:05
2020/08/01 05:30
2020/08/01 06:00
2020/08/01 09:13