Я пытаюсь разобрать следующее json:
"categories": [
{
"id": 42,
"name": "Air Conditioning",
"image_url": "system-data/category/Category-AirConditioning.png",
"image_marker_url": "system-data/category/Marker_Category-Air Conditioning.png",
"status_id": 7,
"created_at": 1580452213,
"updated_at": 1580452213
},
{
"id": 43,
"name": "Car Wash",
"image_url": "system-data/category/Category-Carwash.png",
"image_marker_url": "system-data/category/Marker_Category-Car Wash.png",
"status_id": 7,
"created_at": 1580452213,
"updated_at": 1580452213
},
{
"id": 45,
"name": "Automobile Services",
"image_url": "system-data/category/Category-Automobile.png",
"image_marker_url": "system-data/category/Marker_Category-Automobile Services.png",
"status_id": 7,
"created_at": 1580452213,
"updated_at": 1580452213
},
{
"id": 48,
"name": "Electrical",
"image_url": "system-data/category/Category-Electrical.png",
"image_marker_url": "system-data/category/Marker_Category-Electrical.png",
"status_id": 7,
"created_at": 1580452213,
"updated_at": 1580452213
},
{
"id": 49,
"name": "Generator Repair & Maintenance",
"image_url": "system-data/category/Category-Generator.png",
"image_marker_url": "system-data/category/Marker_Category-Generator Repair & Maintenance.png",
"status_id": 7,
"created_at": 1580452213,
"updated_at": 1580452213
}
]
Ниже моя структура для сопоставления ответа json с моделью:
struct CityCategory: Codable{
let id: Int
let name, imageUrl, imageMarkerUrl: String
let statusID, createdAt, updatedAt: Int
enum CodingKeys: String, CodingKey{
case id = "id"
case name = "name"
case imageUrl = "image_url"
case imageMarkerUrl = "image_marker_url"
case createdAt = "created_at"
case updatedAt = "updated_at"
case statusID = "status_id"
}
}
struct CityCategoryResponse: Codable{
let cityCat: [CityCategory]
}
Я пробуем это:
let response = try JSONDecoder().decode(CityCategoryResponse.self, from: jsonData)
и выдает ошибку.
keyNotFound (CodingKeys (stringValue: "cityCat", intValue: nil), Swift.DecodingError.Context (codingPath: [ ], debugDescription: "Нет значения, связанного с ключом CodingKeys (stringValue: \" cityCat \ ", intValue: nil) (\" cityCat \ ").", underError: nil))