Просто используйте Decodable.
struct Post : Decodable {
var photo : Photo?
}
struct Photo : Decodable {
var source : Source?
}
struct Source : Decodable {
var landscape : String?
}
Alamofire.request("https://api.pexels.com/v1/search?query=Spain+query&per_page=15&page=1", headers: headers).responseJSON { response in
guard let data = response.results.value {
let post = createObject(json: data, decodable: Post.self)
}
}
func createObject<D: Decodable> (json : [String: Any], decodable: D.Type) throws -> D {
return try JSONDecoder().decode(decodable, from: JSONSerialization.data(withJSONObject: snapshot, options: JSONSerialization.WritingOptions.sortedKeys))
}