2016-10-18 3 views
1

Alamofire 및 SwiftyJSON을 사용하여 JSON의 일부 데이터를 구문 분석하지만 이해할 문제가 있습니다. 그래서이Json Swift 3 안에 배열의 첫 번째 항목 만 추가하십시오.

내 질문은
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 받아 추가 할 수 있습니다 내가 Alamofire과 SwiftyJSON으로 그것을 얻을 방법이 인 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" 
    } 
    } 
} 

입니다 빈 배열에 ??? 여러 가지 방법을 시도했지만 처음에는 images 안에 5 개의 소스가 필요합니다. 아이디어가 있으십니까?

답변

2
  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() 

      } 

내가 생각했던 것보다 쉬웠다. 모든 데이터를 배열에 넣은 다음 내부에서 루프를 돌았습니다.

관련 문제