Following is the code which i used Alamofire to call a GET api for getting the train details:
var url:String!
url = "https://api.railwayapi.com/v2/name-number/train/bhopal/apikey/iixp5yxd/"
Alamofire.request(url, method: .get, encoding: JSONEncoding.default)
.responseJSON { response in
switch response.result {
case .success(let json):
print(json)
if let json = response.result.value as? [String:Any] {
print(json["train"]!)
if let nestedDictionary1 = json["train"] as? [String: AnyObject] {
// access individual value in dictionary
if let views = nestedDictionary1["number"] as? String {
print(views)
}
if let classes = nestedDictionary1["classes"] as? [String: AnyObject] {
print(classes)
}
}
}
DispatchQueue.main.async {
// handle your code
}
case .failure(let error):
print(error)
}
}
At the print(json)
line i am getting the following JSON response as:
{
debit = 1;
"response_code" = 200;
train = {
classes = (
{
available = N;
code = 2S;
name = "SECOND SEATING";
},
{
available = N;
code = FC;
name = "FIRST CLASS";
},
{
available = Y;
code = 2A;
name = "SECOND AC";
},
{
available = N;
code = 3E;
name = "3rd AC ECONOMY";
},
{
available = Y;
code = SL;
name = "SLEEPER CLASS";
},
{
available = N;
code = CC;
name = "AC CHAIR CAR";
},
{
available = Y;
code = 3A;
name = "THIRD AC";
},
{
available = Y;
code = 1A;
name = "FIRST AC";
}
);
days = (
{
code = MON;
runs = Y;
},
{
code = TUE;
runs = Y;
},
{
code = WED;
runs = Y;
},
{
code = THU;
runs = Y;
},
{
code = FRI;
runs = Y;
},
{
code = SAT;
runs = Y;
},
{
code = SUN;
runs = Y;
}
);
name = "SHAN-E- BHOPAL EXP";
number = 12156;
};
}
How shall i extract the value of key "Classes" and the corresponding keys of "Classes" . I used this in the above code , but unfortunately its not working:
if let classes = nestedDictionary1["classes"] as? [String: AnyObject] {
print(classes)
}
Since i am a newbie to the Alamofire please tell where i am getting wrong and how to solve this?
Please read the output. JSON has only two collection types, array []
(in the output ()
) and dictionary {}
.
So the value for key classes
is clearly an array ([[String:Any]]
) and JSON unspecified values are always Any
, never AnyObject
.
And please use more descriptive variable names.
if let json = response.result.value as? [String:Any] {
if let train = json["train"] as? [String: Any] {
print(train)
// access individual value in dictionary
if let number = train["number"] as? String { // more likely `Int`
print(number)
}
if let classes = train["classes"] as? [[String: Any]] {
for aClass in classes {
let name = aClass["name"] as! String
let available = aClass["available"] as! String
print(name, available)
}
}
}
}
Nevertheless consider to use the Codable
protocol. It's more convenient and more efficient