2017-03-07 7 views
0

ページネーションをサポートするjsonのURLがあります。 私の質問は、どのようにalamofireとswiftyjsonを使ってこのページページを解析できるのですか?alamofireとswiftyjsonを迅速に使用してページ付けURLを解析する方法は?

私のJSONのURLのように:

{ 
    "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

私はまずあなたが簡単に知るようになると思います。 –

+0

ページネーションの目的は何ですか? 3番目のページを取得するには、すでに2番目のページURLをヒットしなければなりません。 –

答えて

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

私は、データの画像URLを持っている、私はすべてのページページを使用してデータ内のこの画像を解析し、私の配列にこれらの画像を追加したいと思います。 – SwiftyIso

+0

セット全体の各 'data'ノードを解析しますか?だから、データの各セットを得るために最後に達するまで 'nextPageUrl'を使いたいのですか? – dstepan

関連する問題