首页 > 解决方案 > 从 alamofire 获取数据 (responseString)

问题描述

我正在使用此代码检索数据

 Alamofire.request(urlString, method: .post, parameters: parameters,encoding: JSONEncoding.default, headers: headers)
        .responseString() { response in
            switch response.result {
            case .success(let data):
                let jsonData = JSON(data)
                print(jsonData)
                
            case .failure(let error):
                print("\(error) - hello world")
            }
    }

服务器发送的结果是

{"result":"Opertion is successfull"}

但我只想要价值部分"Opertion is successfull"

标签: iosswiftalamofireswift4.1

解决方案


使用 almofire文档中的代码

Alamofire.request(urlString, method: .post, parameters: parameters,encoding: JSONEncoding.default, headers: headers)
    .responseString() { response in
        if let jsonData = response.result.value {
            print("JSON: \(jsonData)") // serialized json response
        }
}

推荐阅读