I imported both Alamofire and SwiftyJSON (latest versions) and I just want to try out the API calls.
here is what I am doing in viewDidLoad:
url: String = "https://api.solvap.com" //this is not the real url of the api
Alamofire.request(url, method: .get)
.validate()
.responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
//this print statement prints the whole JSON list
print(json)
//this print statement just return null for some weird reason
print(json["name"])
case .failure(let error):
print(error)
}
}
Console output in Xcode:
[
{
"name" : "John",
"surname" : "Doe",
"password" : "54321",
"job" : "Developer",
"token" : "iw4lcsk7h8do3y6fuw5vvzefn"
}
]
null
Any thoughts how to fix this and why this happens?
I figured this out.. If the API response looks like this:
(
{
"name" : "John",
"surname" : "Doe",
"password" : "54321",
"job" : "Developer",
"token" : "iw4lcsk7h8do3y6fuw5vvzefn"
}
)
That means the API response is wrong and the perfect way is to fix the response from the Back-End and not trying to parse data from a wrong API response.
There is no reason to waste time on trying to parse data from a wrong API response.
The correct response of the API should look like this:
{
"name" : "John",
"surname" : "Doe",
"password" : "54321",
"job" : "Developer",
"token" : "iw4lcsk7h8do3y6fuw5vvzefn"
}