1
我想解析一些通過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
你解決了你的問題嗎? – Jaydeep