2017-03-07 52 views
0

我有一個支持分頁的json網址。 我的問題是我如何解析這個分頁頁面使用alamofire和swiftyjson?如何在swift中使用alamofire和swiftyjson解析分頁url?

的json的網址,如:

{ 
    "meta":{ 
     "code":200 
    }, 
    "data":{ }, 
    "pagination":{ 
     "total":86, 
     "totalPages":3, 
     "page":1, 
     "nextPage":2, 
     "nextPageUrl":"http://.............?page=2" 
    } 
} 

** !!!!!!! UPDATE !!!!!!! **

我這樣的代碼,但我得到了一些異常:

func GetDataFromUrl(from:String){ 

     Alamofire.request(from, method: .get).validate().responseJSON { response in 
      switch response.result { 
      case .success(let value): 
       let json = JSON(value) 

       self.storeData = [PopulerMagazalarData]() 

         //...Creating Data Obj. 

         let data = PopulerMagazalarData() 

         let username = json["data"]["store"]["user"]["username"].string 
         let userpic = json["data"]["store"]["user"]["profilePicture"].string 
         let productsCount = json["data"]["store"]["productsCount"].int 
         let description = json["data"]["store"]["description"].string 
         let followedby = json["data"]["store"]["user"]["counts"]["followedBy"].int 
         let totalPage = json["pagination"]["totalPages"].int 
         let count:Int? = json["data"]["products"].array?.count 
         if let ct = count { 

          for index in 0...ct-1{ 

          let datas = PopulerMagazalarData() 

          let images = json["data"]["products"][index]["images"]["standart"]["url"].string 

          datas.img1 = images 
          self.storeData.append(datas) 
          } 
         } 


         //***************** 
         data.username = username 
         data.profilPic = userpic 
         data.producsCount = productsCount 
         data.desc = description 
         data.followedby = followedby 

         //****************** 
         self.storeData.append(data) 

       for index in 2 ... totalPage! { 

        self.GetDataFromUrl(from: "https://api......../store/\(username!)?page=\(index)") 
       } 

            // for refresh collecitonView 
        self.refresh_now() 



      case .failure(let error): 
       print(error) 
      } 
     } 
    } 

    //...CollectionView ReloadData func... 
    func refresh_now(){ 

     DispatchQueue.main.async (
      execute: 
      { 
       self.MyStoreCollectionView.reloadData() 
     } 
     ) 

    } 

但是當我滾動我的應用程序下來。

+0

我猜Google可以先輕鬆知道。 –

+0

那麼分頁的目的是什麼?您必須已經點擊第二頁網址才能看到第三頁。 –

回答

0
// make the request with Alamofire 
Alamofire.request("myUrl").response { response in 

    // to create the SwiftyJSON object 
    let json = JSON(data: response.data!) 

    // get the values from the json response 
    let code = json["meta"]["code"].int 
    let total = json["pagination"]["total"].int 

} 
+0

我的意思是,我有一個圖像的網址在數據中,我想解析這個圖像的數據使用所有分頁頁面,並將這些圖像添加到我的數組。 – SwiftyIso

+0

你想解析整個集合的每個'data'節點嗎?所以你想使用'nextPageUrl',直到你到達最後才能獲得每一組數據? – dstepan

相關問題