[swift]抓斗从特定的数组索引 ID

标签: json Swift
发布时间: 2017/3/1 1:31:04
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一些JSON ,将返回以下内容︰

"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名称,如你可以看到,但现在我想要抓住的 json 对象的id 。请帮助。

解决方法 1:

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)")
       }        
    }
官方微信
官方QQ群
31647020