【问题标题】:how to parse pagination url using alamofire and swiftyjson in swift?如何在swift中使用alamofire和swiftyjson解析分页url?
【发布时间】:2017-03-07 18:50:32
【问题描述】:

我有一个支持分页的 json url。 我的问题是如何使用 alamofire 和 swiftyjson 解析这个分页页面?

我的 json 网址如:

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

** !!!!!!!更新!!!!!!!**

我的代码是这样的,但我遇到了一些异常:

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

    }

但是当我滚动我的应用程序时。

【问题讨论】:

  • 我想先谷歌一下你会很容易知道的。
  • 那么分页的目的是什么?您必须已经点击第二页 URL,才能获得第三页。

标签: ios swift parsing alamofire swifty-json


【解决方案1】:
// 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

}

【讨论】:

  • 我的意思是,我在数据中有一个图像 url,我想使用所有分页页面解析数据中的这些图像并将这些图像添加到我的数组中。
  • 您想解析整个集合的每个data 节点吗?所以你想使用nextPageUrl直到你到达最后来获取每组数据?
猜你喜欢
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 1970-01-01
  • 2015-04-18
相关资源
最近更新 更多