Alamofire, Swift 2.0, SwiftyJSON: Parse response b

2019-09-11 11:58发布

问题:

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)