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

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

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

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

Q&A

2回答

1180閲覧

SwiftでJSONをパース

gtime

総合スコア51

Swift

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

0グッド

0クリップ

投稿2017/05/27 10:41

json

1{ 2 "next_cursor" = 0; 3 "next_cursor_str" = 0; 4 "previous_cursor" = 0; 5 "previous_cursor_str" = 0; 6 users = ( 7 { 8 "blocked_by" = 0; 9 blocking = 0; 10 "contributors_enabled" = 0; 11 "created_at" = "Tue Jun 26 01:54:35 +0000 2007"; 12 "default_profile" = 0; 13 "default_profile_image" = 0; 14 description = "\U65e5\U672c\U8a9e\U7248Twitter\U516c\U5f0f\U30a2\U30ab\U30a6\U30f3\U30c8\U3067\U3059\U3002\U30b5\U30fc\U30d3\U30b9\U306b\U95a2\U3057\U3066\U306e\U3054\U8cea\U554f\U3001\U304a\U554f\U3044\U5408\U308f\U305b\U306f https://t.co/mfQkUR3vFO \U304a\U3088\U3073 https://t.co/NZ3xoeBxuz\U3092\U3054\U53c2\U7167\U304f\U3060\U3055\U3044\U3002"; 15 entities = { 16 description = { 17 urls = ( 18 { 19 "display_url" = "support.twitter.com"; 20 "expanded_url" = "https://support.twitter.com/"; 21 indices = ( 22 42, 23 65 24 ); 25 url = "https://t.co/mfQkUR3vFO"; 26 }, 27 { 28 "display_url" = "support.twitter.com/articles/486421"; 29 "expanded_url" = "https://support.twitter.com/articles/486421"; 30 indices = ( 31 70, 32 93 33 ); 34 url = "https://t.co/NZ3xoeBxuz"; 35 } 36 ); 37 }; 38 url = { 39 urls = ( 40 { 41 "display_url" = "blog.jp.twitter.com"; 42 "expanded_url" = "https://blog.jp.twitter.com"; 43 indices = ( 44 0, 45 23 46 ); 47 url = "https://t.co/A9dNuL0CCa"; 48 } 49 ); 50 }; 51 }; 52 "favourites_count" = 507; 53 "follow_request_sent" = 0; 54 "followers_count" = 2286154; 55 following = 0; 56 "friends_count" = 113; 57 "geo_enabled" = 0; 58 "has_extended_profile" = 1; 59 id = 7080152; 60 "id_str" = 7080152; 61 "is_translation_enabled" = 0; 62 "is_translator" = 0; 63 lang = ja; 64 "listed_count" = 16561; 65 "live_following" = 0; 66 location = "\U6771\U4eac\U90fd\U4e2d\U592e\U533a"; 67 muting = 1; 68 name = "Twitter Japan"; 69 notifications = 0; 70 "profile_background_color" = C0DEED; 71 "profile_background_image_url" = "http://pbs.twimg.com/profile_background_images/567482719571959809/WwgdnJCP.jpeg"; 72 "profile_background_image_url_https" = "https://pbs.twimg.com/profile_background_images/567482719571959809/WwgdnJCP.jpeg"; 73 "profile_background_tile" = 1; 74 "profile_banner_url" = "https://pbs.twimg.com/profile_banners/7080152/1493927138"; 75 "profile_image_url" = "http://pbs.twimg.com/profile_images/860218921932046337/ZWvHYKbH_normal.jpg"; 76 "profile_image_url_https" = "https://pbs.twimg.com/profile_images/860218921932046337/ZWvHYKbH_normal.jpg"; 77 "profile_link_color" = 0084B4; 78 "profile_sidebar_border_color" = FFFFFF; 79 "profile_sidebar_fill_color" = DDEEF6; 80 "profile_text_color" = 333333; 81 "profile_use_background_image" = 1; 82 protected = 0; 83 "screen_name" = TwitterJP; 84 "statuses_count" = 5972; 85 "time_zone" = Tokyo; 86 "translator_type" = regular; 87 url = "https://t.co/A9dNuL0CCa"; 88 "utc_offset" = 32400; 89 verified = 1; 90 }, 91 { 92 "blocked_by" = 0; 93 blocking = 0; 94 "contributors_enabled" = 0; 95 "created_at" = "Tue Feb 20 14:35:54 +0000 2007"; 96 "default_profile" = 0; 97 "default_profile_image" = 0; 98 description = "Your official source for what\U2019s happening.\n\nNeed a hand? Visit https://t.co/jTMg7YsLw5"; 99 entities = { 100 description = { 101 urls = ( 102 ); 103 }; 104 url = { 105 urls = ( 106 { 107 "expanded_url" = "<null>"; 108 indices = ( 109 0, 110 23 111 ); 112 url = "https://t.co/gN5JJwhQy7"; 113 } 114 ); 115 }; 116 }; 117 "favourites_count" = 4916; 118 "follow_request_sent" = 0; 119 "followers_count" = 60858805; 120 following = 0; 121 "friends_count" = 161; 122 "geo_enabled" = 1; 123 "has_extended_profile" = 1; 124 id = 783214; 125 "id_str" = 783214; 126 "is_translation_enabled" = 0; 127 "is_translator" = 0; 128 lang = en; 129 "listed_count" = 91032; 130 "live_following" = 0; 131 location = "San Francisco, CA"; 132 muting = 1; 133 name = Twitter; 134 notifications = 0; 135 "profile_background_color" = ACDED6; 136 "profile_background_image_url" = "http://pbs.twimg.com/profile_background_images/657090062/l1uqey5sy82r9ijhke1i.png"; 137 "profile_background_image_url_https" = "https://pbs.twimg.com/profile_background_images/657090062/l1uqey5sy82r9ijhke1i.png"; 138 "profile_background_tile" = 1; 139 "profile_banner_url" = "https://pbs.twimg.com/profile_banners/783214/1494831122"; 140 "profile_image_url" = "http://pbs.twimg.com/profile_images/842992378960986112/Yd1Z53jW_normal.jpg"; 141 "profile_image_url_https" = "https://pbs.twimg.com/profile_images/842992378960986112/Yd1Z53jW_normal.jpg"; 142 "profile_link_color" = 1B95E0; 143 "profile_sidebar_border_color" = FFFFFF; 144 "profile_sidebar_fill_color" = F6F6F6; 145 "profile_text_color" = 333333; 146 "profile_use_background_image" = 1; 147 protected = 0; 148 "screen_name" = Twitter; 149 "statuses_count" = 5067; 150 "time_zone" = "Pacific Time (US & Canada)"; 151 "translator_type" = regular; 152 url = "https://t.co/gN5JJwhQy7"; 153 "utc_offset" = "-25200"; 154 verified = 1; 155 } 156 ); 157}

name = Twitter;
name = "Twitter Japan";

"screen_name" = Twitter;
"screen_name" = TwitterJP;

"profile_image_url_https" = "https://pbs.twimg.com/profile_images/860218921932046337/ZWvHYKbH_normal.jpg";
"profile_image_url_https" = "https://pbs.twimg.com/profile_images/842992378960986112/Yd1Z53jW_normal.jpg";

verified = 1;
verified = 1;

following = 0;
following = 0;

のデータを取り出したいので、このコードを書きましたが、

Swift

1do { 2 if let jsonObject = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary, 3 let user = jsonObject["users"] as? NSDictionary, 4 let name = user["name"] as? String { 5 6 print(name) 7 8 9 } else { 10 print("エラー") 11 } 12 13 14 } catch { 15 16 }

データを取り出すことができません。
どのようにすれば効率よく複数のデータをJSONを取り出すことができるのでしょうか?

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

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

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

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

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

guest

回答2

0

あなたが JSON として提示しているものは JSON ではなく ASCII形式の Apple Property List です。

ですので

swift

1guard let data1 = data, 2 let plist = String(data: data1, encoding: .utf8), 3 let dict = plist.propertyList() as? [String: Any] 4 else { return }

のような形でDictionay<String, Any>が取得できます

投稿2017/05/28 12:58

MasakiHori

総合スコア3384

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

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

0

before

Swift

1 if let jsonObject = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary

after

Swift

1 if let jsonObject = try JSONSerialization.jsonObject(with: data!, options: []) as? [String:Any]

でとりだせないでしょうか? 最近JSONSerializationを使っていないのでウル覚えですが

投稿2017/05/27 23:50

tagoman

総合スコア59

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

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

gtime

2017/05/28 01:40

JSONデータは表示できるのですが、データの取り出しができません。 データの指定方法が間違っているのでしょうか
tagoman

2017/05/28 07:21

取り出しとは変数に入れたいってことでしょうか? よくみたら正しいjson形式じゃないですね。これはtwitterAPIの戻りとかですか?文字列は"で囲む、変数も"で囲む。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問