Вот, я посмотрел много похожих вопросов по этому вопросу, но я просто не могу понять, что мне не хватает.
Мой JSON выглядит так:
{
"Stations": [{
{
"Code": "A02",
"Name": "Farragut North",
"StationTogether1": "",
"StationTogether2": "",
"LineCode1": "RD",
"LineCode2": null,
"LineCode3": null,
"LineCode4": null,
"Lat": 38.903192,
"Lon": -77.039766,
"Address": {
"Street": "1001 Connecticut Avenue NW",
"City": "Washington",
"State": "DC",
"Zip": "20036"
}
}, {
"Code": "A03",
"Name": "Dupont Circle",
"StationTogether1": "",
"StationTogether2": "",
"LineCode1": "RD",
"LineCode2": null,
"LineCode3": null,
"LineCode4": null,
"Lat": 38.909499,
"Lon": -77.04362,
"Address": {
"Street": "1525 20th St. NW",
"City": "Washington",
"State": "DC",
"Zip": "20036"
}
Я установил структуру:
struct AllStations : Codable {
let stations: [String]?
let code: String?
let name: String?
let lat: Double?
let lon: Double?
let lineCode1: String?
let lineCode2: String?
let lineCode3: String?
let lineCode4: String?
let together1: String?
let together2: String?
let address: [String]?
let street: String?
let city: String?
let state: String?
let zip: String?
private enum CodingKeys: String, CodingKey {
case stations = "Stations"
case code = "Code"
case name = "Name"
case lat = "Lat"
case lon = "Lon"
case lineCode1 = "LineCode1"
case lineCode2 = "LineCode2"
case lineCode3 = "LineCode3"
case lineCode4 = "LineCode4"
case together1 = "StationTogether1"
case together2 = "StationTogether2"
case address = "Address"
case street = "Street"
case city = "City"
case state = "State"
case zip = "Zip"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let response = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .stations)
stations = try response.decode([String].self, forKey: .stations)
code = try response.decode(String.self, forKey: .code)
name = try response.decode(String.self, forKey: .name)
lat = try response.decode(Double.self, forKey: .lat)
lon = try response.decode(Double.self, forKey: .lon)
lineCode1 = try response.decode(String.self, forKey: .lineCode1)
lineCode2 = try response.decode(String.self, forKey: .lineCode2)
lineCode3 = try response.decode(String.self, forKey: .lineCode3)
lineCode4 = try response.decode(String.self, forKey: .lineCode4)
together1 = try response.decode(String.self, forKey: .together1)
together2 = try response.decode(String.self, forKey: .together2)
address = try response.decode([String].self, forKey: .address)
let anAddress = try response.nestedContainer(keyedBy: CodingKeys.self, forKey: .address)
street = try anAddress.decode(String.self, forKey: .street)
city = try anAddress.decode(String.self, forKey: .city)
state = try anAddress.decode(String.self, forKey: .state)
zip = try anAddress.decode(String.self, forKey: .zip)
}
}
Я извлекаю данные из URL и передаю их моему декодеру:
func processResponse(using data: Data?) {
if let jsonData = data
{
let decoder = JSONDecoder()
do {
let allStations = try decoder.decode(AllStations.self, from: jsonData)
print(#function, "A Station: ", allStations as Any)
} catch {
print(error.localizedDescription)
}
} else {
// Respond to error
}
}
Но яВозникла проблема с форматированием: «Данные не могут быть прочитаны, поскольку они не в правильном формате».
Я не вижу, что мне не хватает.Помощь будет оценена.