Alamofire JSON 结果用 SwiftyJSON 解析

Alamofire JSON result parse with SwiftyJSON

我不使用 SwiftyJSON 填充 table 视图,它不解析 Alamofire JSON 结果

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

        }

    }

数据示例Here

编辑:此代码有效,但错误原因是我的网络服务错误。感谢帮助!

我就是这么做的:

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

如果我没记错的话,这个模式来自 Alamofire GitHub 示例。我看看能不能找到 link 我想我对这种模式的使用可能起源于 .

这就是我在 Swift 上用 SwiftyJSON 处理 JSON 的方式 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()
    }
}

在请求 JSON 请求时将 content-type headers 设置为 application/json 如下:

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

    }

}