2017-07-26 6 views
0

Ich bin in Swift und SwiftyJSON habe ich gesehen, ist eine gute Bibliothek zu JSON zu parsen, aber immer noch nicht wissen, wie man es richtig verwendet. Ich brauche SwiftyJSON verwenden te Wert von id jeden Benutzer von diesem JSON zu erhalten und sie in ein Array speichern:Wie SwiftyJSON zu implementieren, um diese Daten

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

Könnte mir jemand helfen zu lernen, wie könnte ich es tun? Vielen Dank!

+0

Verwenden Sie Alamofire, um API zu treffen? –

+0

Nein, bin ich nicht. Ich benutze einfach urlRequest, um den JSON zu bekommen, und dann tue ich so, als ob ich die Daten, die ich in dem Beitrag erwähnt habe, bekommen würde. –

Antwort

1

Sie können JSON wie folgt parsen.

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

Das hat perfekt funktioniert! Danke Kumpel! –

+0

@ JavierSanzRozalén Ihre Begrüßung ... – Jaydeep

0

Versuchen Sie diesen Code

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) 
      } 
     } 

Hoffe, es hilft!

Verwandte Themen