如何从post webservice API获取密钥值并将其提取到tableview

问题描述 投票:1回答:1

我以这种格式从API获取数据,但问题是我想从API获得所有问题和答案,但每当我尝试使用键值获取值时,它返回nil值并且应用程序崩溃

这是我的api数据看起来像进入字典this is what my data looks like I want to show the "Options" key data into my tableView

enter image description here

这是我从API获取数据的代码

Alamofire.request(url, method: .post, parameters: parameters,encoding: JSONEncoding.default, headers: header ).responseJSON {
        response in
        switch response.result {
        case .success:

              print(response)

            if let result = response.result.value {

                print(result)

                let responseDict = result as! [String : Any]

                print(responseDict)


              let data = responseDict["Result"] as! [Any] 

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

            print(error)
        }
    }
ios swift api alamofire
1个回答
1
投票

你可以试试

if let res = responseDict["Result"] as? [[String:Any]]  {

    for item in res {

          if let ques = item["Question"] as? String  {

             print(ques)

          }

          if let op = item["Options"] as? [[String:Any]]  {

             print(op)

          }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.