I am using SwiftlyJSON to parse JSON. My JSON looks like this
{
“data”:[{
“id”:123,
“locations”:[{
“lat”:345,
“long”:678
},{
“lat”:345,
“long”:678
}],
”live”:yes
},{
“id”:123,
“locations”:[{
“lat”:999,
“long”:324
},{
“lat”:865,
“long”:765
}],
”live”:no
}],
“success”:true,
“status”: 200
}
I want to get every "lat" and "long" from "locations", pair them and show them in a table cell.
My code in Network Service looks like this
private func updateSearchResults(_ data: Data) {
do {
let json = try JSON(data: data) //successfully parsed data
let locations = json["data"].arrayValue.map {$0["loactions"].arrayObject}
print(locations) //locations array is printing out correctly
for latlang in locations{
if let lat = latlang["lat"]{
print(lat) //ERROR here
}
}
} catch {
print(error)
}
}
Error description: Cannot subscript a value of type '[Any]' with an index of type 'String'
Now, I know the error that I can't access locations array by giving string in index but I don't know how to access the "lat" and "long" from JSON. Any help would be greatly appreciated
if let lat = latlang["late"]{shouldn't it be"lat", dropping thee?