如何从Web服务发布后的API获取关键值并将其获取到TableView

时间:2018-09-05 11:09:25

标签: ios swift api alamofire

我以这种格式从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)
        }
    }

1 个答案:

答案 0 :(得分: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)

          }
    }
}