3

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!

3 Answers3

2

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

    }

}
Dory
  • 7,462
  • 7
  • 33
  • 55
1

I just do this:

let request = Alamofire.request(url, method: method, parameters: params, encoding: URLEncoding.default, headers: httpHeaders).validate()

request.responseJSON { response in
        switch response.result {
        case .success(let json):
            let jsonObject = JSON(json)
            //Use jsonObject
        ....
        }
    }

If I remember correctly, this pattern came from the Alamofire GitHub examples. I'll see if I can find a link I think my use of this pattern probably originated from this answer.

Community
  • 1
  • 1
James Webster
  • 31,873
  • 11
  • 70
  • 114
0

This is how I handle JSON with SwiftyJSON on Swift 3

// handle data
if let value = response.result.value {
    let json = JSON(value)

    for(_, location): (String, JSON) in json{
        let pendingLocation = PendingLocation(_id: location["_id"].stringValue, userId: location["userID"].stringValue, name: location["name"].stringValue)
        self.pendingLocations.append(pendingLocation)
        self.tableView.reloadData()
    }
}
Devbot10
  • 1,193
  • 18
  • 33
  • don't work, please review my example json data. it's [here](http://codebeautify.org/jsonviewer/cb5dda8c) – Eren Özkul Jan 16 '17 at 23:17
  • Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} – Eren Özkul Jan 17 '17 at 00:10
  • @ErenÖzkul, you usually get that error when the service isn't returning JSON. Most often in my case, it's because I'm not getting a 200 HTTP response. – James Webster Jan 19 '17 at 18:15
  • @JamesWebster I know 200 HTTP response, but problem does "content type". I fix it, thanks for help – Eren Özkul Jan 19 '17 at 19:44