I am trying to convert the following JSON from the Met Office to a object in Swift 4 but am running in to errors. My plan is to then store in Core Data once the JSON has been decoded. Here is some of the JSON that is returned
let json = """
{
"Locations":
{
"Location":
[
{
"elevation": "50.0",
"id": "14",
"latitude": "54.9375",
"longitude": "-2.8092",
"name": "Carlisle Airport",
"region": "nw",
"unitaryAuthArea": "Cumbria"
},
{
"elevation": "22.0",
"id": "26",
"latitude": "53.3336",
"longitude": "-2.85",
"name": "Liverpool John Lennon Airport",
"region": "nw",
"unitaryAuthArea": "Merseyside"
}
]
}
}
""".data(using: .utf8)!
I have created a structure that will be used to convert the data into:
struct locations: Decodable {
var Locations: [location]
struct location: Decodable {
var Location: [MetOfficeLocation]
struct MetOfficeLocation: Decodable {
var elevation: String
var id: String
var latitude: String
var longitude: String
var obsSource: String?
var name: String
var region: String
var area: String
private enum CodingKeys: String, CodingKey {
case elevation
case id
case latitude
case longitude
case obsSource
case name
case region
case area = "unitaryAuthArea"
}
}
}
}
I then do the conversion using JSONDecoder:
let place = try JSONDecoder().decode([Location].self, from: json)
for i in place {
print(i.Location[0].name)
}
I am getting a keyNotFound error with No value associated with key locations (\"locations\")." I am confused as i'm not sure what value should be accosted with locations as it is just location
Thank you