JSONのデータの取得ができません。
このようなJSONのデータの"avatar_url"で保存されている値を取得したいです。
[ { "id": 116722761, "node_id": "MDEwOlJlcG9zaXRvcnkxMTY3MjI3NjE=", "name": "abc.github.io", "full_name": "abc/abc.github.io", "private": false, "owner": { "login": "abc", "id": 3063240, "node_id": "MDQ6VXNlcjMwNjMyNDA=", "avatar_url": "https://avatars0.githubusercontent.com/u/3063240?v=4", "gravatar_id": "", "url": "https://api.github.com/users/abc", "html_url": "https://github.com/abc", "followers_url": "https://api.github.com/users/abc/followers", "following_url": "https://api.github.com/users/abc/following{/other_user}", "gists_url": "https://api.github.com/users/abc/gists{/gist_id}", "starred_url": "https://api.github.com/users/abc/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/abc/subscriptions", "organizations_url": "https://api.github.com/users/abc/orgs", "repos_url": "https://api.github.com/users/abc/repos", "events_url": "https://api.github.com/users/abc/events{/privacy}", "received_events_url": "https://api.github.com/users/abc/received_events", "type": "User", "site_admin": false },
そのために書いたコードがこちらです。
func get() { let url = URL(string:"https://api.github.com/users/(searchTextField.text!)/repos") URLSession.shared.dataTask(with: url!) { (data, responds, error) in guard let data = data else {return} do { let json = try? JSON(data: data) let jsonData = json!["owner"][0]["avatar_url"] print("jsonData:(jsonData)") } catch let jsonError{ print("jsonError", jsonError) } }.resume() }
このコードのjson!["owner"][0]["avatar_url"]
が間違っているのかなと思うのですが、どうでしょうか?
ちなみに、JSONのデータの配列自体は取得できています。(デバックエリアにプリントできてます)
import SwiftyJSONもしています。CocoaPodsを利用しています。
よろしくお願いいたします。
あなたの回答
tips
プレビュー