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

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

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

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

Q&A

0回答

1132閲覧

JSONのデータの取得の仕方(Swiftyjson)

dddd12233

総合スコア13

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

0グッド

0クリップ

投稿2020/03/11 03:08

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を利用しています。

よろしくお願いいたします。

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

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

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

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

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

stdio

2020/03/11 03:20

まず、print文でjsonの中身を見た方がいいですね。ownerと言う文字列が単数なのが気になります。
dddd12233

2020/03/11 03:29

jsonの中身をprintするとこんな感じです。 確かに、配列であればownersの方が自然ですよね... json:[ { "stargazers_count" : 0, "notifications_url" : "https:\/\/api.github.com\/repos\/kobayashi\/.macbook-provisioning\/notifications{?since,all,participating}", "git_url" : "git:\/\/github.com\/kobayashi\/.macbook-provisioning.git", "archived" : false, "forks_count" : 0, "forks" : 0, "assignees_url" : "https:\/\/api.github.com\/repos\/kobayashi\/.macbook-provisioning\/assignees{\/user}", "disabled" : false, "contributors_url" : "https:\/\/api.github.com\/repos\/kobayashi\/.macbook-provisioning\/contributors", "default_branch" : "master", "has_issues" : true, "events_url" : "https:\/\/api.github.com\/repos\/kobayashi\/.macbook-provisioning\/events", "pushed_at" : "2017-03-13T22:50:07Z", "owner" : { "login" : "kobayashi", "type" : "User", "gists_url" : "https:\/\/api.github.com\/users\/kobayashi\/gists{\/gist_id}", "repos_url" : "https:\/\/api.github.com\/users\/kobayashi\/repos", "url" : "https:\/\/api.github.com\/users\/kobayashi", "id" : 8712070, "received_events_url" : "https:\/\/api.github.com\/users\/kobayashi\/received_events", "following_url" : "https:\/\/api.github.com\/users\/kobayashi\/following{\/other_user}", "subscriptions_url" : "https:\/\/api.github.com\/users\/kobayashi\/subscriptions", "organizations_url" : "https:\/\/api.github.com\/users\/kobayashi\/orgs", "avatar_url" : "https:\/\/avatars1.githubusercontent.com\/u\/8712070?v=4", "node_id" : "MDQ6VXNlcjg3MTIwNzA=", "gravatar_id" : "", "starred_url" : "https:\/\/api.github.com\/users\/kobayashi\/starred{\/owner}{\/repo}", "site_admin" : false, "events_url" : "https:\/\/api.github.com\/users\/kobayashi\/events{\/privacy}", "html_url" : "https:\/\/github.com\/kobayashi", "followers_url" : "https:\/\/api.github.com\/users\/kobayashi\/followers" },
hoshi-takanori

2020/03/11 03:31

一番外側に配列があり、owner は配列ではないので json![0]["owner"]["avatar_url"] では。
dddd12233

2020/03/11 03:52

ありがとうございます。 ご指摘箇所直したら取得できました。 感謝いたします。
takabosoft

2020/03/11 06:05

hoshi-takanoriさん、回答欄へどうぞ。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問