2016-11-15 79 views
0

我从链接中导入JSON,其中每个文件都包含一个带有下一个JSON文件的URL的“下一个”属性,直到它最终为空并且已经遍历所有文件。解析连续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 
} 

编辑更新:在注释应用的代码了个去,这是我当前的代码,但我仍然有问题:

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个对象(通过组合您的请求),则不会有第三个页面。只需检查'next'的值,然后重做。我建议把'url'放在'getData'参数中,如果是'.success',请检查'if let next = data [“next”],self.getData(URL.init(data [下一个“])completionHandler:sameCompletionHandler)(在伪代码中)。 – Larme

+0

以任何方式看到这适用于实际的代码? im很难适应它,以适应我的要求没有错误 – infernouk

+0

我显然不是一个Swift开发人员,但我设法做一些似乎工作:'print(“请求成功\(url)”);让json =数据为! [字符串:任何];让results = json [“results”] as! [[字符串:任何]]; completionHandler(results,nil);如果让nextURL = json [“next”]为? String {self.getData(_url:nextURL,completionHandler:completionHandler)} else {print(json [“next”]); print(“没有下一页,我们在最后”)}'我确定它不是Swift安全的,但你应该明白。我给'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) 
     } 
    } 
} 
}