将响应主体解析为JSON

将响应主体解析为JSON

本文介绍了Alamofire,Swift 2.0,SwiftyJSON:将响应主体解析为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功完成了对服务器的POST请求,并且尝试解析响应JSON,但未成功。

I have successfully completed a POST request to server and I am trying to parse the response JSON but I have been unsuccessful.

        Alamofire.request(.POST, ServerConfig.ADD_SELLER_URL, parameters: sellerJSON, encoding: .JSON, headers: nil)
        .responseJSON(completionHandler: { responseRequest, responseResponse, responseResult in
            print(responseRequest!.URL)
            print(responseResponse)
            print(responseResult)

            let json = JSON(responseResponse!)
            print(json)
        })

我正在使用SwiftyJSON进行JSON解析。
这是我的输出

I am using SwiftyJSON for JSON parsing.Here is my output

Optional(http://stage-sellers.strawmine.com/api/v1/sellers/addSeller)
Optional(<NSHTTPURLResponse: 0x7f8f6df20530> { URL: http://stage-sellers.strawmine.com/api/v1/sellers/addSeller } { status code: 400, headers {
Connection = "keep-alive";
"Content-Type" = "application/json; charset=utf-8";
Date = "Mon, 12 Oct 2015 10:32:35 GMT";
Server = "nginx/1.4.6 (Ubuntu)";
"Transfer-Encoding" = Identity;
} })
SUCCESS
unknown

如您所见,仅打印响应标题。另外,如果我打印变量json,我将变得未知。如果我打印json.stringValue我得到一个空字符串。我想从正文中获取JSON数据。请帮忙!

As you can see, only the response headers are printed. Also, I am getting 'unknown' if I print the variable json. If I print json.stringValue I get an empty string. I want to get the JSON data from the body. Please help! Thanks in advance!

推荐答案

您需要使用 responseResult.value!

let json = JSON(responseResult.value!)
print(json)

这篇关于Alamofire,Swift 2.0,SwiftyJSON:将响应主体解析为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 23:14