尝试使用SwiftyJSON解析JSON数据3使用SwiftyJSON

问题描述:

我想解析一些通过API调用获得的JSON数据。我首先尝试了艰难的方式:没有图书馆。如果这不起作用,我尝试了SwiftyJSON,但这也没有什么帮助。我试图从啤酒中获得所有的子信息。尝试使用SwiftyJSON解析JSON数据3使用SwiftyJSON

在这里你可以看到我打印出整个JSON'对象'的位置。

斯威夫特

let json = JSON(data: JSONData) 
print(json); 

输出

{ 
    "meta" : { 
    "init_time" : { 
     "measure" : "seconds", 
     "time" : 0.011 
    }, 
    "response_time" : { 
     "measure" : "seconds", 
     "time" : 0.056 
    }, 
    "code" : 200 
    }, 
    "notifications" : [ 

    ], 
    "response" : { 
    "macro" : { 
     "count" : 10, 
     "items" : [ 
     { 
      "beer" : { 
      "wish_list" : false, 
      "beer_abv" : 5.4, 
      "auth_rating" : 0, 
      "style_name" : "Witbier", 
      "beer_label" : "https:\/\/untappd.akamaized.net\/site\/beer_logos\/beer-3839_207a3_sm.jpeg", 
      "bid" : 3839, 
      "beer_name" : "Belgian White", 
      "beer_style" : "Witbier" 
      }, 
      "total_count" : 668, 
      "your_count" : 0, 
      "brewery" : { 
      "location" : { 
       "lng" : -104.992, 
       "lat" : 39.7561, 
       "brewery_city" : "Denver", 
       "brewery_state" : "CO" 
      }, 
      "brewery_name" : "Blue Moon Brewing Company", 
      "brewery_slug" : "blue-moon-brewing-company", 
      "brewery_id" : 127804, 
      "country_name" : "United States", 
      "brewery_label" : "https:\/\/untappd.akamaized.net\/site\/brewery_logos\/brewery-127804_1a670.jpeg", 
      "contact" : { 
       "twitter" : "BlueMoonBrewCo", 
       "facebook" : "https:\/\/www.facebook.com\/bluemoon", 
       "url" : "http:\/\/www.bluemoonbrewingcompany.com\/", 
       "instagram" : "" 
      }, 
      "brewery_active" : 1 
      } 
     }, 

在这里,我去了具体的分层次的。

夫特

let json = JSON(data: JSONData) 
let list = json["response","micro","items"] 
print(list) 

输出

[ 
    { 
    "beer" : { 
     "wish_list" : false, 
     "beer_abv" : 4.7, 
     "auth_rating" : 0, 
     "style_name" : "IPA - Session \/ India Session Ale", 
     "beer_label" : "https:\/\/untappd.akamaized.net\/site\/beer_logos\/beer-36834_82ca8_sm.jpeg", 
     "bid" : 36834, 
     "beer_name" : "All Day IPA", 
     "beer_style" : "IPA - Session \/ India Session Ale" 
    }, 
    "total_count" : 548, 
    "your_count" : 0, 
    "brewery" : { 
     "location" : { 
     "lng" : -85.6735, 
     "lat" : 42.9585, 
     "brewery_city" : "Grand Rapids", 
     "brewery_state" : "MI" 
     }, 
     "brewery_name" : "Founders Brewing Co.", 
     "brewery_slug" : "founders-brewing-co", 
     "brewery_id" : 549, 
     "country_name" : "United States", 
     "brewery_label" : "https:\/\/untappd.akamaized.net\/site\/brewery_logos\/brewery-foundersbrewingcompany_549.jpeg", 
     "contact" : { 
     "twitter" : "foundersbrewing", 
     "facebook" : "http:\/\/www.facebook.com\/foundersbrewing", 
     "url" : "http:\/\/www.foundersbrewing.com\/", 
     "instagram" : "foundersbrewing" 
     }, 
     "brewery_active" : 1 
    } 
    }, 

当我加入 “啤酒”。我得到了一个“空” - 返回。我认为不是因为方括号的不同。 “[]”=列表和“{}”=词典。但我仍然无法解决它。

(我是不是一个iOS开发者,只是他与swift3的首次互动的学生)

斯威夫特

let json = JSON(data: JSONData) 
let list = json["response","micro","items","beer"] 
print(list) 

输出

null 
+0

你解决了你的问题吗? – Jaydeep

可以使用解析响应对象像这样的SwiftyJSON。而你的items对象是啤酒阵列。希望你能明白你的想法。

let json = JSON(data: JSONData) 
let list = json["response"]["micro"]["items"].arrayValue 

for beerObjects in list { 

     // You can get sub info of beer like this. 
     let beer_name = beerObjects["beer"]["beer_name"].stringValue 
     let total_count = beerObjects["total_count"].intValue 
     let brewery_city = beerObjects["brewery"]["location"]["brewery_city"] 

     // or parse information which you need 
}