2016-11-15 5 views
0

最終的にnullですべてのファイルを通過するまで、各ファイルに次のJSONファイルのURLを含む「次の」プロパティが含まれるリンクからJSONをインポートしています。連続したJSONファイルを解析する最善の方法は?

私の質問は、どのように私はこれらの連続したファイルをすべてインポートすることができますか?それらはすべてテーブルに必要ですが、制限はAPI制限に従ってJSONあたり20個のオブジェクトです。

答えは、結果をループして 'オブジェクトの数が20で、URLページ番号を1ずつインクリメントするとしたらどうなるでしょうか?一度私は最後のページをヒットし、8つの結果を持っているそれは別のループに行くことを知っているでしょうか?私はちょうどこのコードがどのように機能し、どこに座っているのか理解できません。

現在の要求:

open class ApiService: NSObject { 

open func getData(completionHandler: @escaping (NSDictionary?, NSError?) -> Void) -> Self { 

    let requestUrl = "https://wger.de/api/v2/exercise/?format=json&language=2&status=2&?limit=199" 

    Alamofire.request(requestUrl, method: .get, encoding: URLEncoding.default) 
     .responseJSON { response in 

      switch response.result { 

      case .success(let data): 
       print("Request was sucessful") 
       completionHandler(data as? NSDictionary, nil) 

      case .failure(let error): 
       print("Request failed with error: \(error)") 
       completionHandler(nil, error as NSError?) 
      } 
    } 
    return self 
} 

EDITのUPDATE:コメントでコードを適用で行くを持っていたが、これは私の現在のコードですが、私はまだ問題があります(あなたのコードを更新

let requestUrl = "https://wger.de/api/v2/exercise/?format=json&language=2&status=2" 

open func getData(_URL: NSURL, completionHandler: @escaping (NSDictionary?, NSError?) -> Void) -> Self { 

Alamofire.request(requestUrl, method: .get, encoding: URLEncoding.default) 
    .responseJSON { response in 

     switch response.result { 

     case .success(let data): 
      print("Request was sucessful") 

      let json = data as! [String:Any] 

      let results = json["results"] as! NSDictionary; completionHandler(results, nil) 

      if let nextURL = json["next"] as? NSURL {self.getData(_URL: nextURL, completionHandler: completionHandler)} else { print(json["next"] as? String) 

       print("No next page, we are at the end") 
      } 

     case .failure(let error): 
      print("Request failed with error: \(error)") 
      completionHandler(nil, error as NSError?) 
     } 
} 
return self 
+0

オブジェクトを数えないでください。あなたの要求の組み合わせによってのみ40のオブジェクトがある場合、3番目のページはありません。 'next'の値をチェックして、それをやり直してください。私は 'getData'のparamに' url'を入れ、 '.success'の場合は' let next = data ["next"]、self.getData(URL.init(data [" next "])completionHandler:sameCompletionHandler)'(擬似コードで)。 – Larme

+0

実際のコードにこれを適用する方法はありますか?私の要求をエラーなく適合させるのに苦労している – infernouk

+0

私は明らかにスウィフトの開発者ではありませんが、うまくいくような何かをすることができました: 'print("リクエストは\(url)で成功しました " json = dataとしましょう! [文字列:任意];結果をjson ["results"]としましょう! [[文字列:任意]]; completionHandler(results、nil); let nextURL = json ["next"]? String {self.getData(_url:nextURL、completionHandler:completionHandler)} else {print(json ["next"]);私はそれが速い安全ではないと確信していますが、あなたはその考えを得るべきです。私は 'getData'にパラメータurlを追加しました。 – Larme

答えて

1

を私はそれを編集していません)

open class ApiService: NSObject { 

open func getData(requestUrl: String, completionHandler: @escaping (NSDictionary?, NSError?) -> Void) -> Self { 

    Alamofire.request(requestUrl, method: .get, encoding: URLEncoding.default) 
     .responseJSON { response in 

      switch response.result { 

      case .success(let data): 
       print("Request was sucessful") 
       completionHandler(data as? NSDictionary, nil) 

      case .failure(let error): 
       print("Request failed with error: \(error)") 
       completionHandler(nil, error as NSError?) 
      } 
    } 
    return self 
} 
} 

ビューコントローラのコード

override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     let initialURLString = "https://wger.de/api/v2/exercise/?format=json&language=2&status=2" 
     getApiData(dataURL:initialURLString) 
    } 

func getApiData(dataURL: String) { 

let _ = apiService.getData(requestUrl: dataURL) { 
    (data, error) in 
    if let data = data { 
     if let results = data["results"] as? [[String:Any]] { 
      for result in results { 
       if let exercise = Exercise(dictionary: result) { 
        self.exercises.append(exercise) 
       } 
      } 
      self.exercisesTableView.reloadData() 
     } 
     if let nextURL = data["next"] as? String 
     { 
      print("NEXT URL: \(nextURL)") 
      self.getApiData(dataURL:nextURL) 
     } 
    } 
} 
} 
関連する問題