Вы можете попробовать это:
Alamofire.request(url, method: .get)
.responseJSON { response in
if let value = response.value {
let json = JSON(value).arrayValue
if let name = json[0]["group"].string {
print(name!)
}
}
}
Для отправки запроса с параметрами вы можете использовать это
func getResult(url:String, paramKey:[String], paramValue:[Any], completion: @escaping (Bool, Any?) -> Void) {
let _headers : HTTPHeaders = ["Content-Type":"application/x-www-form-urlencoded"]
let params : Parameters = getParams(paramKey: paramKey, paramValue: paramValue)
guard let url = URL(string: url) else {
completion(false, nil)
return
}
Alamofire.request(url,
method: .post,
parameters: params, encoding: URLEncoding.httpBody , headers: _headers)
.validate()
.responseJSON { response in
guard response.result.isSuccess else {
completion(false, nil)
return
}
if let value = response.result.value{
let json = JSON(value)
if json["status_code"].stringValue == "200" {
completion(true, json)
} else {
completion(false, json)
}
}
}
}
func getParams(paramKey:[String], paramValue:[Any]) -> [String:Any] {
var dictionary = [String:Any]()
dictionary.updateValue(Constants.API_TOKEN, forKey: HTTPParams.PARAM_API_TOKEN)
for index in 0..<paramKey.count {
dictionary.updateValue(paramValue[index], forKey: paramKey[index])
}
return dictionary
}
А из ViewController вы можете использовать это для вызова
let url = "Your API Url"
let params = ["occupation"] // Param key
let paramValues = ["doctor"] // Param values
// UrlRequest the file name where the method is placed
UrlRequest().getResult(url: url, paramKey: params, paramValue: paramValues) { (success, data) in
if success {
// Success result with data
}else{
// Failed
}
}