Ios 使用Alamofire Swift将JSON结果传递给第二个Web服务以获得最终结果

Ios 使用Alamofire Swift将JSON结果传递给第二个Web服务以获得最终结果,ios,json,swift,alamofire,Ios,Json,Swift,Alamofire,我有两个web服务并使用Alamofire 第一个web服务是.get方法,我已经得到了JSON结果 对于第二个web服务是.post方法,我需要将第一个web服务JSON结果传递给最终数据列表的参数 想要实现:将第一个web服务json数据传递给第二个web服务(参数)或任何获得最终数据的建议。 请任何人帮忙 第一个Web服务: Alamofire.request("http://GetConstantTableList", method: .get, encoding: encoding

我有两个web服务并使用Alamofire

  • 第一个web服务是
    .get
    方法,我已经得到了JSON结果
  • 对于第二个web服务是
    .post
    方法,我需要将第一个web服务JSON结果传递给最终数据列表的参数
想要实现:将第一个web服务json数据传递给第二个web服务(参数)或任何获得最终数据的建议。 请任何人帮忙

第一个Web服务:

Alamofire.request("http://GetConstantTableList", method: .get, encoding: encoding, headers: [ "Accept":"application/json", "Authorization":"Bearer \(token ?? "")"])
.responseJSON { respo in
print(respo)
结果第一个Web服务:

{
    "items": [
        {
            "actionType": 101,
            "version": 1
        },
        {
            "actionType": 1015,
            "version": 1
        }

        ]
}
Alamofire.request("http://GetConstantTableData", method: .post, parameters: ???  encoding: encoding, headers: [ "Accept":"application/json", "Authorization":"Bearer \(token ?? "")"])
.responseJSON { response in
print(response)
}
第二个Web服务:

{
    "items": [
        {
            "actionType": 101,
            "version": 1
        },
        {
            "actionType": 1015,
            "version": 1
        }

        ]
}
Alamofire.request("http://GetConstantTableData", method: .post, parameters: ???  encoding: encoding, headers: [ "Accept":"application/json", "Authorization":"Bearer \(token ?? "")"])
.responseJSON { response in
print(response)
}

通过在第一个api的响应块内调用第二个api,可以直接传递第一个api的响应

private func callFirstApi() {
    Alamofire.request("http://GetConstantTableList", method: .get, encoding: encoding, headers: [ "Accept":"application/json", "Authorization":"Bearer \(token ?? "")"])
        .responseJSON { response in
            switch response.result {
            case .success(let value):
                if let parameters = value as? [String: Any] {
                    callSecondApi(with: parameters)
                }

            case .failure(let error):
                print(error.localizedDescription)
            }
    }
}

private func callSecondApi(with parameters: [String: Any]) {
    Alamofire.request("http://GetConstantTableData", method: .post, parameters: parameters, encoding: encoding, headers: [ "Accept":"application/json", "Authorization":"Bearer \(token ?? "")"])
        .responseJSON { response in
            print(response)
    }
}

是否要按从第一个api获取的数据的原样传递数据?@Aakash是的。我已经应用了您的代码,如果让参数=响应为,则
?[String:Any]
给出错误
错误:
从“DataResponse”转换为不相关类型“[String:Any]”总是失败