I need help to read json in ios swift. I know i can use decodable but this is an exemple of my json. When i try xith decodable, i always get a nil variable.
Thnaks for your help
[
{
"name": "Bob",
"age": "16",
"employed": "No"
},
{
"name": "Vinny",
"age": "56",
"employed": "Yes"
}
]
This is my code, with decodable
struct personResult: Decodable{
var person: [Person]
}
struct Person:Decodable{
var name: String
var age: String
var employed: String
}
and This is my function
func getJson() {
let url = URL(string: myUrl)!
let task = URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
if let error = error {
print("Error : \(error)")
return
}
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
print("Error with the response, unexpected status code: \(response)")
return
}
if let data = data {
print(data)
do {
let result = try JSONDecoder().decode(personResult.self, from: data)
print(result)
}catch _ {
print("errror during json decoder")
}
}
})
task.resume()
}
}
I always enter on my catch, and printed error message