質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

0回答

1075閲覧

URLからデータを取得したい

totot

総合スコア20

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/05/12 23:22

編集2019/05/13 00:51

fetchVenueを呼び出したところ
keyNotFound(CodingKeys(stringValue: "group", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "response", intValue: nil)], debugDescription: "No value associated with key CodingKeys(stringValue: "group", intValue: nil) ("group").", underlyingError: nil))
というエラーが出てしまい、データを取得できません。
解決方法を教えていただけないでしょうか?

Swift

1class Service { 2 3 static let shared = Service() 4 5 6 func fetchVenue(location: CLLocationCoordinate2D, completion: @escaping (Empty?, Error?) -> ()) { 7 8 let urlString = "https://api.foursquare.com/v2/search/recommendations?ll=(location.latitude),(location.longitude)&v=20160607&intent=coffee&limit=15&client_id=(client_id)&client_secret=(client_secret)" 9 10 fetchGenericJSONData(urlString: urlString, completion: completion) 11 } 12 13 func fetchGenericJSONData<T: Codable>(urlString: String, completion: @escaping (T?, Error?) -> ()) { 14 15 guard let url = URL(string: urlString) else { return } 16 URLSession.shared.dataTask(with: url) { (data, resp, err) in 17 if let err = err { 18 completion(nil, err) 19 return 20 } 21 do { 22 let objects = try JSONDecoder().decode(T.self, from: data!) 23 // success 24 completion(objects, nil) 25 } catch { 26 completion(nil, error) 27 } 28 }.resume() 29 } 30}

Json

1{ 2 "meta":{ 3 "code":200, 4 "requestId":"5cd6fa5e9fb6b75700851759" 5 }, 6 "response":{ 7 "group":{ 8 "results":[ 9 { 10 "displayType":"venue", 11 "venue":{ 12 "id":"4c4fde46dcb5d13ad7b10108", 13 "name":"Fruit de saison (フルーフ・デゥ・セゾン)", 14 "contact":{ 15 16 }, 17 "location":{ 18 "address":"外神田4-11-2", 19 "lat":35.702276108565556, 20 "lng":139.77286384637375, 21 "labeledLatLngs":[ 22 { 23 "label":"display", 24 "lat":35.702276108565556, 25 "lng":139.77286384637375 26 } 27 ], 28 "distance":223, 29 "postalCode":"101-0021", 30 "cc":"JP", 31 "city":"東京", 32 "state":"東京都", 33 "country":"日本", 34 "formattedAddress":[ 35 "外神田4-11-2", 36 "千代田区, 東京都", 37 "101-0021", 38 "日本" 39 ] 40 }, 41 "categories":[ 42 { 43 "id":"4bf58dd8d48988d16d941735", 44 "name":"Café", 45 "pluralName":"Cafés", 46 "shortName":"Café", 47 "icon":{ 48 "prefix":"https://ss3.4sqi.net/img/categories_v2/food/cafe_", 49 "suffix":".png" 50 }, 51 "primary":true 52 } 53 ], 54 "verified":false, 55 "stats":{ 56 "tipCount":0, 57 "usersCount":0, 58 "checkinsCount":0, 59 "visitsCount":0 60 }, 61 "dislike":false, 62 "ok":false, 63 "beenHere":{ 64 "count":0, 65 "lastCheckinExpiredAt":0, 66 "marked":false, 67 "unconfirmedCount":0 68 } 69 }, 70 "id":"5cd6fa5fa795bd0039c46dfc", 71 "photo":{ 72 "id":"51bfd28d498e80f211f96907", 73 "createdAt":1371525773, 74 "prefix":"https://fastly.4sqi.net/img/general/", 75 "suffix":"/52296049_SkW5N14j2phk0gmnmQ3XGhBFCjy5KJkak2fhB7tKju4.jpg", 76 "width":720, 77 "height":960, 78 "visibility":"public" 79 }, 80 "snippets":{ 81 "count":1, 82 "items":[ 83 { 84 "detail":{ 85 "type":"tip", 86 "object":{ 87 "id":"56dc0836498ee85362455de5", 88 "createdAt":1457260598, 89 "text":"great selection of heavenly yummy desserts", 90 "type":"user", 91 "canonicalUrl":"https://foursquare.com/item/56dc0836498ee85362455de5", 92 "logView":true, 93 "agreeCount":0, 94 "disagreeCount":0, 95 "todo":{ 96 "count":0 97 }, 98 "user":{ 99 "id":"11175627", 100 "firstName":"Roldan", 101 "lastName":"Toribio", 102 "gender":"male", 103 "photo":{ 104 "prefix":"https://fastly.4sqi.net/img/user/", 105 "suffix":"/ZDDPKA4LIZFCVYQ4.jpg" 106 } 107 } 108 } 109 } 110 } 111 ] 112 } 113 } 114 ], 115 "totalResults":112 116 }, 117 "context":{ 118 "searchLocationNearYou":true, 119 "searchLocationMapBounds":false, 120 "searchLocationType":"NearYou", 121 "currentLocation":{ 122 "what":"", 123 "where":"", 124 "feature":{ 125 "cc":"JP", 126 "name":"Akihabara", 127 "displayName":"Akihabara, Tokyo, Tōkyō", 128 "woeType":22, 129 "slug":"akihabara-japan", 130 "id":"maponics:10004338", 131 "longId":"10004338", 132 "geometry":{ 133 "center":{ 134 "lat":35.701304, 135 "lng":139.775174 136 }, 137 "bounds":{ 138 "ne":{ 139 "lat":35.706567, 140 "lng":139.780981 141 }, 142 "sw":{ 143 "lat":35.6968, 144 "lng":139.769614 145 } 146 } 147 }, 148 "encumbered":true 149 }, 150 "parents":[ 151 152 ] 153 }, 154 "boundsSummaryRadius":600, 155 "relatedNeighborhoods":[ 156 157 ], 158 "geoParams":{ 159 "ll":"35.7020691,139.7753269", 160 "radius":"600" 161 }, 162 "geoBounds":{ 163 "circle":{ 164 "center":{ 165 "lat":35.7020691, 166 "lng":139.7753269 167 }, 168 "radius":600 169 } 170 } 171 } 172 } 173}

Swift

1struct Empty: Codable { 2 let response: Response 3} 4struct Response: Codable { 5 let group: Group 6} 7struct Group: Codable { 8 let results: [Result] 9 let totalResults: Int 10} 11struct Result: Codable { 12 let venue: Venue 13} 14struct Venue: Codable { 15 let id, name: String 16 let contact: Contact 17 let location: Location 18 let categories: [Category] 19 let verified: Bool 20 let dislike, ok: Bool 21 let beenHere: BeenHere 22} 23struct BeenHere: Codable { 24 let count, lastCheckinExpiredAt: Int 25 let marked: Bool 26 let unconfirmedCount: Int 27} 28struct Category: Codable { 29 let id, name, pluralName, shortName: String 30 let primary: Bool 31} 32struct Contact: Codable { 33} 34struct Location: Codable { 35 let address: String 36 let lat, lng: Double 37 let distance: Int 38 let postalCode, cc, city, state: String 39 let country: String 40 let formattedAddress: [String] 41} 42

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問