如何实现SwiftyJSON来获取这些数据

时间:2017-07-26 12:03:44

标签: ios json swift

我已经进入Swift并且我已经看到SwiftyJSON是一个很好的解析JSON的库,但仍然不知道如何正确使用它。我需要使用SwiftyJSON从这个JSON中获取每个用户的id值,并将它们保存到一个数组中:

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

有人能帮我学习怎么办? 非常感谢你!

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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)
            }
        }

希望它有所帮助!