使用Alamofire从api获取响应时出现问题

时间:2018-07-14 09:38:19

标签: ios swift alamofire

我正在将详细信息api集成到我的应用程序中,但是我无法理解如何从数组中获取数据,因此我将响应放在这里以更好地理解

 {
"success": "1",
"data": {
    "service_list": [
        {
      "service_id": "11",
      "fk_user_id": "77",
      "fk_bus_id": "0",
      "fk_sub_cate_id": "1",
      "service_name": "Business Cards",
       "service_desc": "Business Cards",
       "service_price": "500",
       "service_img1": "https://www.kwikmypay.com/mode_share/modeshare_admin/assets/images/business/ser_pquat1531379189.png",
       "service_img2": "https://www.kwikmypay.com/mode_share/modeshare_admin/assets/images/business/ser_hjzty1531379189.png",
       "service_img3": "https://www.kwikmypay.com/mode_share/modeshare_admin/assets/images/business/ser_rmgxb1531379189.png",
       "service_img4": "https://www.kwikmypay.com/mode_share/modeshare_admin/assets/images/business/ser_kptyq1531379189.png",
       "service_img5": "https://www.kwikmypay.com/mode_share/modeshare_admin/assets/images/business/ser_xwmlq1531379189.png"
        }
    ]
},
"message": ""
}

这是我的回复,我想在表格视图中打印服务列表数组,请问有人可以帮忙

这是我的答案

   let busdetail = ["user_id":u_id,"access_token":acc_tkn,"bus_id":busid] as [String:Any]
        print(busdetail)
        Alamofire.request(businessDetail, method: .post, parameters: busdetail).responseJSON
            {
                response in

                print(response)
                let result = response.result

                if let dict = result.value as? Dictionary<String,AnyObject>{
                    if let serlist = dict["data"]{
                        if let s_list = serlist["service_list"]{

                        self.servicelist = s_list as! [AnyObject]
                        self.tbl_expand.reloadData()
                        //self.tbl_servicelist.reloadData()
                        //self.dropDown.items = categorylist as! [AnyObject] as! [String]
                        }
                    }
                }
                self.hideLoader()
                //self.activityIndicator.stopAnimating()
        }

1 个答案:

答案 0 :(得分:0)

func demoApi() {
Alamofire.request("yourapi", method: .post, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

    switch(response.result) {
    case .success(_):
        if let json= response.result.value as? [String:Any] {   
        if let response = json["data"] as? [String:Any],
        let ServiceList= response["service_list"] as? [[String:Any]] {

     for servicelist in ServiceList {
        let service_id= servicelist ["service_id"] as! String
        let service_name= servicelist ["service_name"] as! String
        let service_img1= servicelist ["service_img1"] as! String

    }
  }
}

            DispatchQueue.main.async {
                self.yourTableView.reloadData()
            }
        }
        break

    case .failure(_):
        print("Error")
        break

    }
}

 }