Try casting json
to [String: Any]
before using it.
Also you seem to have an error here: if let couname = country["countryname"] as? [AnyObject]
You should cast it to an array of [String: AnyObject]
: [[String: AnyObject]]
The adjusted code would look like:
do {
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String: Any]
if let countries = json["Countries"] as? [[String: AnyObject]] {
for country in countries {
if let couname = country["countryname"] as? [AnyObject] {
country_names.append(couname)
}
if let coucode = country["code"] as? [AnyObject] {
country_codes.append(coucode)
}
}
}
} catch {
print("Error Serializing JSON: \(error)")
}