iosswiftalamofireswift4.1

get Data from alamofire (responseString)


I am retrieving the data with this code

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

and the result that has sent by the server is

{"result":"Opertion is successfull"}

but i just want the value part "Opertion is successfull"


Solution

  • Use the code from the almofire documentation

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