仅在 Json Swift 3 中附加数组的第一项
Posted
技术标签:
【中文标题】仅在 Json Swift 3 中附加数组的第一项【英文标题】:Append only the first item of an Array inside Json Swift 3 【发布时间】:2016-10-18 14:20:22 【问题描述】:我正在使用 Alamofire 和 SwiftyJSON 来解析 JSON 中的一些数据,但我有一个理解问题。 所以这是 JSON
"data" : [
"images" : [
"source" : "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-9\/14680504_1200113096725648_1728625782476314570_n.jpg?oh=a20884ad66bef170b7afa9d4f109abf1&oe=585F830B",
"width" : 349,
"height" : 557
,
"source" : "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-0\/p320x320\/14680504_1200113096725648_1728625782476314570_n.jpg?oh=effc78bc088150bc762a79d7b0cfad6c&oe=5860B857",
"width" : 320,
"height" : 510
,
"source" : "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-0\/p180x540\/14680504_1200113096725648_1728625782476314570_n.jpg?oh=e014f2b98eb8152dbfd84b6fdfefc25b&oe=58913E1C",
"width" : 338,
"height" : 540
,
"source" : "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-0\/p130x130\/14680504_1200113096725648_1728625782476314570_n.jpg?oh=ce0430df1e0e605b2b33542634b70adb&oe=58A3A2B0",
"width" : 130,
"height" : 207
,
"source" : "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-0\/p75x225\/14680504_1200113096725648_1728625782476314570_n.jpg?oh=18ed8685d773104637f8a747b21783af&oe=58689107",
"width" : 140,
"height" : 225
],
"id" : "1200113096725648"
,
"images" : [
"source" : "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-9\/14732411_1196819920388299_7949002046479842067_n.jpg?oh=7b7a61f956594f0015dea746640f3e33&oe=589B36EA",
"width" : 294,
"height" : 171
,
"source" : "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-0\/p130x130\/14732411_1196819920388299_7949002046479842067_n.jpg?oh=fe74c57b606ef817aed805707fe6fe6d&oe=58A96851",
"width" : 223,
"height" : 130
],
"id" : "1196819920388299"
,
"images" : [
"source" : "https:\/\/scontent.xx.fbcdn.net\/t31.0-8\/14753242_1194616067275351_4639120157874462599_o.jpg",
"width" : 1920,
"height" : 1200
,
"source" : "https:\/\/scontent.xx.fbcdn.net\/t31.0-8\/p960x960\/14753242_1194616067275351_4639120157874462599_o.jpg",
"width" : 1536,
"height" : 960
,
"source" : "https:\/\/scontent.xx.fbcdn.net\/t31.0-8\/p720x720\/14753242_1194616067275351_4639120157874462599_o.jpg",
"width" : 1152,
"height" : 720
,
"source" : "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-9\/14650295_1194616067275351_4639120157874462599_n.jpg?oh=19248c58a8a11a4506b364aa44a7dc4a&oe=58A4E3F0",
"width" : 960,
"height" : 600
,
"source" : "https:\/\/scontent.xx.fbcdn.net\/t31.0-0\/p480x480\/14753242_1194616067275351_4639120157874462599_o.jpg",
"width" : 768,
"height" : 480
,
"source" : "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-0\/p320x320\/14650295_1194616067275351_4639120157874462599_n.jpg?oh=672575780d6ec2c46c7fc0b25421941c&oe=589800AC",
"width" : 512,
"height" : 320
,
"source" : "https:\/\/scontent.xx.fbcdn.net\/t31.0-0\/p180x540\/14753242_1194616067275351_4639120157874462599_o.jpg",
"width" : 864,
"height" : 540
,
"source" : "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-0\/p130x130\/14650295_1194616067275351_4639120157874462599_n.jpg?oh=f6441f7dde33aec00785f7f96dea31b5&oe=58A1D94B",
"width" : 208,
"height" : 130
,
"source" : "https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-0\/p75x225\/14650295_1194616067275351_4639120157874462599_n.jpg?oh=bbe7ab23b13269a062bfcfa0afcce8e1&oe=589A7EFC",
"width" : 360,
"height" : 225
],
"id" : "1194616067275351"
],
"paging" :
"cursors" :
"after" : "MTE5NDYxNjA2NzI3NTM1MQZDZD",
"before" : "MTIwMDExMzA5NjcyNTY0OAZDZD"
这就是我使用 Alamofire 和 SwiftyJSON 获得它的方式
Alamofire.request("https://graph.facebook.com/\(eventId1)/photos", method: .get, parameters: ["fields": "images", "access_token": "token"])
.responseJSON (responseData) -> Void in
if((responseData.result.value) != nil)
let swiftyJsonVar = JSON(responseData.result.value!)
print(swiftyJsonVar)
我的问题是,如何在 JSON 中获取每个 images
数组的第一个 source
并将其附加到一个空数组?
我尝试了几种方法,但它总是在第一个 images
中使用 5 个源。
有什么想法吗?
【问题讨论】:
【参考方案1】: if let images = swiftyJsonVar["data"].array
for i in 0...images.count-1
let source = images[i]["images"][0]["source"].string
DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async
if let url = NSURL(string: source!),
let data = NSData(contentsOf: url as URL)
DispatchQueue.main.async(execute:
self.sliderPinakas.append(UIImage(data:data as Data)!)
self.makeSlider()
);
else
self.sliderPinakas.append(UIImage(named: "noimageinside")!)
self.makeSlider()
这比我想象的要容易。 只需将所有数据放入一个数组中,然后在其中循环。
【讨论】:
以上是关于仅在 Json Swift 3 中附加数组的第一项的主要内容,如果未能解决你的问题,请参考以下文章