ios - 读取来自 Json Post Call Swift 的响应

标签 ios swift swift2

java 应用程序中,我们使用下面的代码从 REST 获取字符串形式的响应。

String response = performPostCall(wmeAPI.vote("" + rowItem.getPoll_id()), has);

我正在用 Alamofire 快速调用

我打了邮寄电话

Alamofire.request(.POST, url, parameters: parameters, encoding:.JSON).responseString
            { response in switch response.result {
                case .Success(let JSON):
                    print("Success \(JSON)")
                case .Failure(let error):
                    print("Request failed with error: \(error)")
                    }
        }

如何从这个 post 调用中获取响应字符串。

最佳答案

您需要使用响应 JSON

因此,将 responseString 更改为 responseJSON

例如:

Alamofire.request(.GET, "YOUR_URL").responseJSON { (responseData) -> Void in
    if((responseData.result.value) != nil) {
        let swiftyJsonVar = JSON(responseData.result.value!)
        print(swiftyJsonVar)
    }
}

对于 POST 调用:

Alamofire.request(.POST, "YOUR_URL", parameters: nil, encoding: ParameterEncoding.JSON, headers: nil).responseJSON { (responseObject) -> Void in

    print(responseObject)

    if responseObject.result.isSuccess {
        let resJson = JSON(responseObject.result.value!)
        print(resJson)
    }
    if responseObject.result.isFailure {
        let error : NSError = responseObject.result.error!
        print(error)
    }
}

关于ios - 读取来自 Json Post Call Swift 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36809603/

相关文章:

ios - 用于在 native Facebook iPhone 应用程序中共享 URL 的自定义 URL 方案

ios - FirebaseUI 安装问题 (iOS-Swift)

ios - fatal error :使用JSON文件和SwiftyJSON解开可选值时意外发现nil

ios - 在键盘显示或隐藏时调整 UIView

ios - Ionic SVG 图像在 iOS 上运行缓慢

ios - Xcode 6 Storyboard Unwind Segue with Swift 不连接退出

json - SWIFT DateFormatter 结果不一致

ios - 找不到 swift darwin 文档

swift - Decodable 类型的数组不可解码

快速 Alamofire 响应 .jsp