Search code examples
jsonswiftxcodealamofireswifty-json

Alamofire JSON result parse with SwiftyJSON


I don't populate table view with SwiftyJSON, it's not parse Alamofire JSON result

let getRouteURL = "http://example.com/api/Trafi/GetRoute/"
    Alamofire.request(getRouteURL, method: HTTPMethod.post, parameters: param, encoding: JSONEncoding.default, headers:nil).responseJSON{ response in

        if let result = response.result.value {
            if let JSON = try? JSON(result){
                if let RoutesArr = JSON["Routes"].arrayObject{
                    self.routes = RoutesArr as! [[String : AnyObject]]
                    self.routeTable.reloadData()
                }
            }

        }

    }

Data Example Here

Edit: This code is working but error reason my web service error. Thanks for help!


Solution

  • Set content-type headers to application/json while requesting JSON request as below :

    let headers = [
                        "Content-Type" : "application/json; charset=UTF-8"
                  ]
    
    let getRouteURL = "http://example.com/api/Trafi/GetRoute/"
    
    Alamofire.request(getRouteURL, 
      method: HTTPMethod.post, 
      parameters: param, 
      encoding: JSONEncoding.default, 
      headers:headers).responseJSON{ response in
    
        if let result = response.result.value {
            if let JSON = try? JSON(result){
                if let RoutesArr = JSON["Routes"].arrayObject{
                    self.routes = RoutesArr as! [[String : AnyObject]]
                    self.routeTable.reloadData()
                }
            }
    
        }
    
    }