2017-02-14 59 views
1

我有一些JSON其返回以下結果:抓鬥從特定的數組索引的ID

"data": [ 
{ 
    "created_time": "2010-09-03T16:07:14+0000", 
    "name": "Profile Pictures", 
    "id": "125287297520173" 
}, 
{ 
    "created_time": "2010-12-03T00:05:31+0000", 
    "name": "Mobile Uploads", 
    "id": "146617845387118" 
}, 
{ 
    "created_time": "2013-07-27T11:34:50+0000", 
    "name": "Timeline Photos", 
    "id": "546011742114391" 
}, 
{ 
    "created_time": "2017-01-04T19:02:40+0000", 
    "name": "Untitled Album", 
    "id": "1178578645524361" 
}, 
{ 
    "created_time": "2016-09-10T18:26:25+0000", 
    "name": "Untitled Album", 
    "id": "1076646985717528" 
}, 
{ 
    "created_time": "2016-07-06T18:27:09+0000", 
    "name": "OS X Photos", 
    "id": "1033031426745751" 
}, 
{ 
    "created_time": "2013-06-22T07:32:01+0000", 
    "name": "iOS Photos", 
    "id": "530462737002625" 
}, 
{ 
    "created_time": "2012-05-22T19:01:42+0000", 
    "name": "Cover Photos", 
    "id": "370987619616805" 
}, 
{ 
    "created_time": "2015-08-27T18:59:56+0000", 
    "name": "Untitled Album", 
    "id": "879780692070826" 
}, 
{ 
    "created_time": "2014-12-06T16:13:01+0000", 
    "name": "DRMC 2005 Batch", 
    "id": "761469943901902" 
}, 
{ 
    "created_time": "2013-06-16T09:01:17+0000", 
    "name": "Instagram Photos", 
    "id": "528368577212041" 
}, 
{ 
    "created_time": "2012-09-09T17:37:55+0000", 
    "name": "Liverpool Exclusive", 
    "id": "416230538425846" 
}, 
{ 
    "created_time": "2012-09-10T16:31:52+0000", 
    "name": "LIVERPOOL FC TOUR", 
    "id": "416540875061479" 
}, 
{ 
    "created_time": "2010-06-11T19:37:20+0000", 
    "name": "cars", 
    "id": "104577376257832" 
}, 
{ 
    "created_time": "2011-03-29T23:50:18+0000", 
    "name": "Camera+ Photos", 
    "id": "174268382622064" 
} 
] 

我想搶字段「名」的ID:「我的照片」 。我使用SwiftyJSON投的類型,到目前爲止,我成功地做到這一點:

let dictionary = JSON(result) 
// print("albums **************\(dictionary)") 
if let data = dictionary["data"].array { 
print("data of profilePicture ******* \(data)") 
let index = data.index{ $0["name"] == "Profile Pictures" } 

    } 
} 

我可以detech的 profilePicture,你可以看到,但現在我要搶ID該json對象的。請幫忙。

+1

您想獲取只有一個** ** ID然後'VAR身份識別碼:字符串? =(yourArrayName [0] [「id」] as?String)'希望這會對你有所幫助。或者你可以通過for循環獲取它。 –

+0

是@Mukesh我試過了,我也得到了預期的結果,但是我怎麼能確定** JSON **會返回** [0] th **索引我想要的ID,這可能會返回其他指標。這就是爲什麼我確保我抓住了**身份證** **其中**名稱:個人資料圖片**,因爲我想** ** ID **。 –

回答

0
let dictionary = JSON(result) 
// print("albums ID are **************\(dictionary)") 
if let data = dictionary["data"].array { 
print("data of profilePicture ******* \(data)") 

if let dict = data.first(where: { ($0["name"].string) == "Profile Pictures" }) { 
     let id = dict["id"] 
     print("my desired id : ********* \(id)") 
     }   
    } 
1

使用filter讓你感興趣的事物...字典

let dictionary = JSON(result) 
// print("albums **************\(dictionary)") 
if let data = dictionary["data"].array { 
    print("data of profilePicture ******* \(data)") 

    if let dict = data.filter{ $0["name"] == "Profile Pictures" }.first as? [String: String] { 
     let id = dict["id"] 
    } 
} 
+0

雖然你的答案幫助我解決了這個問題,但還是有一些問題,謝謝 –

+2

將一個鏈接的'filter'和'first'應用於非懶惰集合(例如,如上所述的數組)通常是浪費的,你可以直接使用['first(where:)'](https://developer.apple.com/reference/swift/array/1848165-first)(直接使用'filter'中使用的謂詞)來代替,短路。 – dfri

+0

不錯!你每天都會學到新東西:D –