Спасибо всем за ответы, особенно @ dahiya_boy
Я настроил код, отправленный @dahiya_boy, и получил именно то, что мне нужно.
Нижекод.Я создал jsonString
в качестве примера.
import Foundation
class MyModelClass: Codable {
let busArray: [Bus]
enum CodingKeys: String, CodingKey {
case busArray = "result"
}
init(busArray: [Bus]) {
self.busArray = busArray
}
}
class Bus: Codable {
let lat, lon: Double
let time, lines, brigade: String
enum CodingKeys: String, CodingKey {
case lat = "Lat"
case lon = "Lon"
case time = "Time"
case lines = "Lines"
case brigade = "Brigade"
}
init(lat: Double, lon: Double, time: String, lines: String, brigade: String) {
self.lat = lat
self.lon = lon
self.time = time
self.lines = lines
self.brigade = brigade
}
}
var jsonString = """
{
"result":[
{
"Lat":52.276408,
"Lon":21.167618,
"Time":"2018-08-24 11:50:05",
"Lines":"225",
"Brigade":"4"
},
{
"Lat":52.222656,
"Lon":21.102633,
"Time":"2018-08-24 11:51:03",
"Lines":"225",
"Brigade":"2"
},
{
"Lat":52.2100185,
"Lon":21.2054211,
"Time":"2018-08-24 11:51:08",
"Lines":"119",
"Brigade":"2"
},
{
"Lat":52.1676735,
"Lon":21.2222606,
"Time":"2018-08-24 11:51:07",
"Lines":"213",
"Brigade":"3"
}
]
}
"""
if let jsonData = jsonString.data(using: .utf8) {
let decodedJSON = try! JSONDecoder().decode(MyModelClass.self, from: jsonData)
print("Latitude: \(decodedJSON.busArray[0].lat), Longitude: \(decodedJSON.busArray[0].lon), Line: \(decodedJSON.busArray[0].lines)")
}
Это печатает следующий вывод консоли:
Latitude: 52.276408, Longitude: 21.167618, Line: 225