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

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

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

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

2回答

535閲覧

取得したJSONデータへのアクセス方法を知りたい

退会済みユーザー

退会済みユーザー

総合スコア0

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2022/04/12 09:11

編集2022/04/13 04:55

前提

TwitterAPIを使用してRTした人を取得しようとしています。
JSONデータは取得できたのですがそのデータを扱おうとするとエラーになります。

該当ツイート:https://twitter.com/TwitterJP/status/1512360290192883712

【エラーメッセージ】 json[i][screenName]の中身は・・ <INVALID JSON>

該当のソースコード

Swift

1for i in 0..<json.array!.count { 2 let retweeters = Tweeters(name: json[i]["name"].string ?? "ユーザーネームはありません", screenName: json[i]["screen_name"].string ?? "スクリーンネームはありません", profile_image_url: json[i]["profile_image_url"].string ?? "no_image") 3 print("json[i][screen_name]の中身は・・") 4 print(json[i]["screen_name"]) 5} 6 7
jsonの中身は・・ [ { "in_reply_to_user_id_str" : null, "retweeted_status" : { "retweet_count" : 120.0, "in_reply_to_user_id_str" : null, "in_reply_to_screen_name" : null, "favorited" : false, "in_reply_to_status_id" : null, "favorite_count" : 613.0, "id" : 1.5123602901928837e+18, "retweeted" : false, "id_str" : "1512360290192883712", "text" : "金曜日よ、多くの人が君を待っていたぞ", "lang" : "ja", "coordinates" : null, "geo" : null, "contributors" : null, "entities" : { "user_mentions" : [ ], "hashtags" : [ ], "symbols" : [ ], "urls" : [ ] }, "is_quote_status" : false, "truncated" : false, "source" : "<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>", "in_reply_to_status_id_str" : null, "created_at" : "Fri Apr 08 09:22:46 +0000 2022", "in_reply_to_user_id" : null, "place" : null, "user" : { "profile_background_image_url_https" : "https://abs.twimg.com/images/themes/theme1/bg.png", "profile_sidebar_border_color" : "FFFFFF", "profile_sidebar_fill_color" : "DDEEF6", "id" : 7080152.0, "profile_link_color" : "1B95E0", "profile_image_url_https" : "https://pbs.twimg.com/profile_images/1354485696392613893/9T6ogVOI_normal.jpg", "lang" : null, "favourites_count" : 1049.0, "friends_count" : 108.0, "profile_background_color" : "C0DEED", "default_profile" : false, "entities" : { "url" : { "urls" : [ { "display_url" : "blog.twitter.com/ja_jp.html", "url" : "https://t.co/IgW6OEkIbG", "expanded_url" : "https://blog.twitter.com/ja_jp.html", "indices" : [ 0.0, 23.0 ] } ] }, "description" : { "urls" : [ ] } }, "location" : "東京都中央区", "protected" : false, "name" : "Twitter Japan", "url" : "https://t.co/IgW6OEkIbG", "id_str" : "7080152", "geo_enabled" : true, "statuses_count" : 14635.0, "description" : "What is Happening?! 今日も何かが起きている?!", "is_translator" : false, "profile_text_color" : "333333", "notifications" : false, "time_zone" : null, "profile_use_background_image" : true, "profile_banner_url" : "https://pbs.twimg.com/profile_banners/7080152/1647824977", "followers_count" : 2283978.0, "profile_image_url" : "http://pbs.twimg.com/profile_images/1354485696392613893/9T6ogVOI_normal.jpg", "verified" : true, "profile_background_image_url" : "http://abs.twimg.com/images/themes/theme1/bg.png", "following" : false, "translator_type" : "regular", "profile_background_tile" : true, "is_translation_enabled" : false, "listed_count" : 14847.0, "contributors_enabled" : false, "screen_name" : "TwitterJP", "utc_offset" : null, "default_profile_image" : false, "created_at" : "Tue Jun 26 01:54:35 +0000 2007", "has_extended_profile" : true, "withheld_in_countries" : [ ], "follow_request_sent" : false } }, "in_reply_to_screen_name" : null, "is_quote_status" : false, "user" : { "profile_link_color" : "1DA1F2", "is_translation_enabled" : false, "followers_count" : 502.0, "contributors_enabled" : false, "geo_enabled" : false, "has_extended_profile" : true, "lang" : null, "description" : "", "profile_background_image_url_https" : null, "created_at" : "Tue Jun 22 06:37:40 +0000 2021", "withheld_in_countries" : [ ], "default_profile" : true, "following" : false, "is_translator" : false, "time_zone" : null, "verified" : false, "profile_text_color" : "333333", "default_profile_image" : false, "id" : 1.407226176892932e+18, "listed_count" : 0.0, "favourites_count" : 15669.0, "entities" : { "description" : { "urls" : [ ] } }, "profile_background_tile" : false, "notifications" : false, "name" : "mau 🐌 ia", "statuses_count" : 22481.0, "protected" : false, "friends_count" : 486.0, "profile_background_image_url" : null, "profile_use_background_image" : true, "profile_image_url_https" : "https://pbs.twimg.com/profile_images/1502988579290509314/vaqDinyV_normal.jpg", "profile_sidebar_border_color" : "C0DEED", "translator_type" : "none", "screen_name" : "cheolsauriki", "profile_banner_url" : "https://pbs.twimg.com/profile_banners/1407226176892932096/1648038723", "url" : null, "profile_background_color" : "F5F8FA", "follow_request_sent" : false, "profile_sidebar_fill_color" : "DDEEF6", "location" : " 💭 that prince from okayama | mainly 엔하                       \nany pronouns — selective ¡ sf to unf. ", "id_str" : "1407226176892932096", "profile_image_url" : "http://pbs.twimg.com/profile_images/1502988579290509314/vaqDinyV_normal.jpg", "utc_offset" : null }, "favorited" : false, "place" : null, "created_at" : "Tue Apr 12 04:05:17 +0000 2022", "geo" : null, "coordinates" : null, "text" : "RT @TwitterJP: 金曜日よ、多くの人が君を待っていたぞ", "retweet_count" : 120.0, "in_reply_to_status_id" : null, "in_reply_to_status_id_str" : null, "source" : "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>", "id" : 1.5137299468162458e+18, "lang" : "ja", "retweeted" : false, "id_str" : "1513729946816245763", "in_reply_to_user_id" : null, "favorite_count" : 0.0, "contributors" : null, "entities" : { "symbols" : [ ], "hashtags" : [ ], "user_mentions" : [ { "id" : 7080152.0, "id_str" : "7080152", "name" : "Twitter Japan", "indices" : [ 3.0, 13.0 ], "screen_name" : "TwitterJP" } ], "urls" : [ ] }, "truncated" : false } ]

試したこと

・【Swift入門】SwiftでJSONを扱ってみよう!
https://www.sejuku.net/blog/34776

コメント

該当ツイートをリツイートした人のJSONデータ各種値を取得したいです

  • screen_name
  • name

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

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

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

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

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

guest

回答2

0

ベストアンサー

自己解決しました。

参考:https://qiita.com/Enchan/items/50de231cf4494476ca15

投稿2022/04/14 09:26

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

0

json[i]["screenName"]ではなく
json[i]["screen_name"]ではないでしょうか

投稿2022/04/12 10:52

5ugarVVatch1ng

総合スコア356

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

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

退会済みユーザー

退会済みユーザー

2022/04/13 04:54

ではないです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問