Alamofire, Swift 2.0, SwiftyJSON: Parse response body as JSON

早过忘川 提交于 2019-12-25 03:35:36

问题


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)
        })

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

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!


回答1:


You need to use responseResult.value! to get your json data.

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


来源:https://stackoverflow.com/questions/33079107/alamofire-swift-2-0-swiftyjson-parse-response-body-as-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!