Javier Sanz Rozalén Javier Sanz Rozalén - 1 year ago 36
JSON Question

How to implement SwiftyJSON to get this data

I've been getting into Swift and I have seen SwiftyJSON is a good library to parse JSON, but still don't know how to use it properly. I need to use SwiftyJSON to get te value of id of every user from this JSON and save them into an array:

{
"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
},...


Could anybody help me to learn how could I do it ?
Thank you very much!

Answer Source

You can parse json like this way.

var IDs: [String] = []

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

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

  print(IDs)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download