2017-07-26 5 views
0

私はSwiftに入ってきましたが、SwiftyJSONはJSONを解析するのに適したライブラリですが、それを正しく使用する方法はまだ分かりません。私は、このJSONからすべてのユーザーのIDのTE値を取得し、配列に保存するSwiftyJSONを使用する必要があります。このデータを取得するためにSwiftyJSONを実装する方法

{ 
    "users": [ 
    { 
     "id": 2960784075, 
     "id_str": "2960784075", 
     "name": "Jesus Rafael Abreu M", 
     "screen_name": "chuomaraver", 
     "location": "", 
     "profile_location": null, 
     "url": null, 
     "description": "", 
     "protected": false, 
     "followers_count": 1, 
     "friends_count": 101, 
     "listed_count": 0, 
     "created_at": "Sun Jan 04 19:58:06 +0000 2015", 
     "favourites_count": 0, 
     "utc_offset": null, 
     "time_zone": null, 
     "geo_enabled": false, 
     "verified": false, 
     "statuses_count": 0, 
     "lang": "es", 
     "contributors_enabled": false, 
     "is_translator": false, 
     "is_translation_enabled": false, 
     "profile_background_color": "C0DEED", 
     "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", 
     "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", 
     "profile_background_tile": false, 
     "profile_image_url": "http://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png", 
     "profile_image_url_https": "https://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png", 
     "profile_link_color": "0084B4", 
     "profile_sidebar_border_color": "C0DEED", 
     "profile_sidebar_fill_color": "DDEEF6", 
     "profile_text_color": "333333", 
     "profile_use_background_image": true, 
     "default_profile": true, 
     "default_profile_image": true, 
     "following": false, 
     "follow_request_sent": false, 
     "notifications": false, 
     "muting": false 
    }, 
    { 
     "id": 2959453074, 
     "id_str": "2959453074", 
     "name": "lama masarwa", 
     "screen_name": "LamaMasarwa", 
     "location": "", 
     "profile_location": null, 
     "url": null, 
     "description": "", 
     "protected": false, 
     "followers_count": 7, 
     "friends_count": 53, 
     "listed_count": 0, 
     "created_at": "Mon Jan 05 10:10:46 +0000 2015", 
     "favourites_count": 0, 
     "utc_offset": null, 
     "time_zone": null, 
     "geo_enabled": false, 
     "verified": false, 
     "statuses_count": 2, 
     "lang": "he", 
     "contributors_enabled": false, 
     "is_translator": false, 
     "is_translation_enabled": false, 
     "profile_background_color": "C0DEED", 
     "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", 
     "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", 
     "profile_background_tile": false, 
     "profile_image_url": "http://pbs.twimg.com/profile_images/552047388873859072/itk5cPx6_normal.png", 
     "profile_image_url_https": "https://pbs.twimg.com/profile_images/552047388873859072/itk5cPx6_normal.png", 
     "profile_banner_url": "https://pbs.twimg.com/profile_banners/2959453074/1420454122", 
     "profile_link_color": "0084B4", 
     "profile_sidebar_border_color": "C0DEED", 
     "profile_sidebar_fill_color": "DDEEF6", 
     "profile_text_color": "333333", 
     "profile_use_background_image": true, 
     "default_profile": true, 
     "default_profile_image": false, 
     "following": false, 
     "follow_request_sent": false, 
     "notifications": false, 
     "muting": false 
    },... 

誰もが、私はそれを行うことができる方法を学ぶために私を助けてもらえますか? ありがとうございます!

+0

あなたはAPIを打つためにalamofireを使用していますか? –

+0

いいえ、私はそうではありません。私はちょうどJSONを取得するためにurlRequestを使用し、私はポストで言及したデータを取得するふりをします。 –

答えて

1

このようにjsonを解析できます。

var IDs: [String] = [] 

//json is object where users are available 
let users = json["users"].arrayValue 

users.forEach({ 
    IDs.append($0["id"].stringValue) 
}) 

    print(IDs) 
+0

これは完璧に機能しました!ありがとう! –

+0

@JavierSanzRozalénあなたの歓迎... – Jaydeep

0

let json = "{ \"users\": [{ \"id\": 2960784075, \"id_str\": \"2960784075\" }, { \"id\": 2959453074, \"id_str\": \"2959453074\" } ] }"; 
     if let data = json.data(using: String.Encoding.utf8) { 
      let json = JSON(data: data) 

      for item in json["users"].arrayValue { 
       print(item["id"].stringValue) 
      } 
     } 

はそれが役に立てば幸い!このコードを試してみてください

関連する問題