Search code examples
jsongetalamofire

Alamofire 3.0 handle JSON


Sorry for my bad english :)

So here we go. I started to create iPhone Apps in Swift 2. I would like to use an API. For the Request I have used the Alamofire 3.0 Library. And this works fine, but I am unable to handle the JSON. I tried to use SwiftyJSON but I have no idea how it works. There is my code:

let headers = [
    "Content-Type": "application/json"
]

    Alamofire.request(.GET, "API URL", headers: headers)
        .responseJSON { response in

            if response.result.isSuccess {

            }

    }

I hope someone can help me. ;) Thanks


Solution

  • My latest usage of Alamofire (3.0) and SwiftyJSON (2.3)

    let parameters = ["param1" : param, "param2" : "stringParam"] // my params, not required
    Alamofire.request(.POST, url, parameters: parameters)
        .responseJSON{ response in
            guard response.result.error == nil else {
                print("Error. \(response.result.error?.localizedDescription)")
                    return
                } // guard close
                if response.result.isSuccess {
                    let post = JSON(response.result.value!) // JSON is stored in post variable
                // Another check of result
                //if let value: AnyObject = response.result.value {
                //    let post = JSON(value)
                } // if close
         } // responseJSON close
    

    Then access the JSON as per the instructions in Github