2017-02-14 37 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 –