预期对Array进行解码 但找到了字典-Swift Codable

问题描述 投票:0回答:1

我正在尝试调试来自Codable的错误消息,但是在代码中的任何地方都看不到错误。希望有人可以提供帮助。

我的数据模型如下:

final class Amenity : Codable {
    var id: String
    var hasChildren: Bool
    var name: String
    var fullName: String?
    var phone: String?
    var redirectLink: String?
    var moreInfoLink: String?
    var description: String?
    var businessDays: [BusinessDays] = []
    var children: [Amenity] = []

    enum CodingKeys : String, CodingKey {
        case id = "id"
        case businessDays = "businessdays"
        case hasChildren = "hasChild"
        case name = "name"
        case phone = "phoneNumer"
        case moreInfoLink = "moreInfoLink"
        case redirectLink = "redirectLink"
        case fullName = "fullName"
        case description = "desc"
        case children = "children"
    }

    required init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        id = try values.decode(String.self, forKey: .id)
        businessDays = try values.decodeIfPresent([BusinessDays].self, forKey: .businessDays) ?? [BusinessDays]()
        name = try values.decode(String.self, forKey: .name)
        children = try values.decodeIfPresent([Amenity].self, forKey: .children) ?? [Amenity]()
        hasChildren = try values.decodeIfPresent(Bool.self, forKey: .hasChildren) ?? false
        phone = try values.decodeIfPresent(String.self, forKey: .phone)
        fullName = try values.decodeIfPresent(String.self, forKey: .fullName)
        redirectLink = try values.decodeIfPresent(String.self, forKey: .redirectLink)
        description = try values.decodeIfPresent(String.self, forKey: .description)
        moreInfoLink = try values.decodeIfPresent(String.self, forKey: .moreInfoLink)

    }
}

final class BusinessDays: Codable {
    var name: String?
    var hours: [Hour]
    var daysOfWeek: String
    var displayName: String?
}

final class Hour: Codable {
    var name: String?
    var startTime: String
    var endTime: String
    var groupName: String
}

JSON是这样的:

 [
        {
            "businessdays": [],
            "_id": "5e18e270147424000e337acc",
            "fullName": "MV - Dining",
            "name": "Dining & Cafés",
            "phoneNumber": "",
            "createdAt": "2020-01-10T20:45:36.459Z",
            "updatedAt": "2020-01-14T22:25:01.066Z",
            "__v": 0,
            "campus": "5e00fe557ecc0b000e1c0fa1",
            "hasChild": true,
            "children": [
                {
                    "businessdays": [
                        {
                            "hours": [
                                {
                                    "_id": "5e13d9389f25816614f84563",
                                    "name": "Breakfast-7-10",
                                    "startTime": "7 AM",
                                    "endTime": "10 AM",
                                    "createdAt": "2020-01-07T01:04:56.255Z",
                                    "updatedAt": "2020-01-13T19:46:44.667Z",
                                    "__v": 0,
                                    "groupName": "Breakfast",
                                    "id": "5e13d9389f25816614f84563"
                                },
                                {
                                    "_id": "5e1cc2ebd40bf0000e83af64",
                                    "name": "Lunch-1130-230",
                                    "startTime": "11:30 AM",
                                    "endTime": "2:30 PM",
                                    "groupName": "Lunch",
                                    "createdAt": "2020-01-13T19:20:11.513Z",
                                    "updatedAt": "2020-01-13T19:46:54.041Z",
                                    "__v": 0,
                                    "id": "5e1cc2ebd40bf0000e83af64"
                                },
                                {
                                    "_id": "5e1cc311d40bf0000e83af65",
                                    "name": "Dinner-430-7",
                                    "startTime": "4:30 PM",
                                    "endTime": "7 PM",
                                    "groupName": "Dinner",
                                    "createdAt": "2020-01-13T19:20:49.468Z",
                                    "updatedAt": "2020-01-13T19:47:27.836Z",
                                    "__v": 0,
                                    "id": "5e1cc311d40bf0000e83af65"
                                }
                            ],
                            "_id": "5e13d9479f25816614f84564",
                            "name": "El Camino Cafe - Weekday",
                            "daysOfWeek": "2,3,4,5,6",
                            "displayName": "Weekday",
                            "createdAt": "2020-01-07T01:05:11.305Z",
                            "updatedAt": "2020-01-13T19:45:18.357Z",
                            "__v": 0,
                            "id": "5e13d9479f25816614f84564"
                        },
                        {
                            "hours": [
                                {
                                    "_id": "5e13d9389f25816614f84563",
                                    "name": "Breakfast-7-10",
                                    "startTime": "7 AM",
                                    "endTime": "10 AM",
                                    "createdAt": "2020-01-07T01:04:56.255Z",
                                    "updatedAt": "2020-01-13T19:46:44.667Z",
                                    "__v": 0,
                                    "groupName": "Breakfast",
                                    "id": "5e13d9389f25816614f84563"
                                },
                                {
                                    "_id": "5e1cc2ebd40bf0000e83af64",
                                    "name": "Lunch-1130-230",
                                    "startTime": "11:30 AM",
                                    "endTime": "2:30 PM",
                                    "groupName": "Lunch",
                                    "createdAt": "2020-01-13T19:20:11.513Z",
                                    "updatedAt": "2020-01-13T19:46:54.041Z",
                                    "__v": 0,
                                    "id": "5e1cc2ebd40bf0000e83af64"
                                },
                                {
                                    "_id": "5e1cc311d40bf0000e83af65",
                                    "name": "Dinner-430-7",
                                    "startTime": "4:30 PM",
                                    "endTime": "7 PM",
                                    "groupName": "Dinner",
                                    "createdAt": "2020-01-13T19:20:49.468Z",
                                    "updatedAt": "2020-01-13T19:47:27.836Z",
                                    "__v": 0,
                                    "id": "5e1cc311d40bf0000e83af65"
                                }
                            ],
                            "_id": "5e1cc278d40bf0000e83af63",
                            "name": "El Camino Cafe - Weekend",
                            "daysOfWeek": "1,7",
                            "createdAt": "2020-01-13T19:18:16.931Z",
                            "updatedAt": "2020-01-13T19:44:39.106Z",
                            "__v": 0,
                            "displayName": "Weekend",
                            "id": "5e1cc278d40bf0000e83af63"
                        }
                    ],
                    "_id": "5e0a4fb06f894a44bbc0d238",
                    "name": "El Camino Café",
                    "phoneNumber": "",
                    "createdAt": "2019-12-30T19:27:44.544Z",
                    "updatedAt": "2020-01-14T21:27:39.516Z",
                    "__v": 0,
                    "campus": "5e00fe557ecc0b000e1c0fa1",
                    "campusName": "Mountain View",
                    "desc": "The cafeteria is located on the ground floor of the new main hospital. Salads, sandwiches, hot meals, breakfast items, desserts, beverages and snacks are available during hours noted below.",
                    "fullName": "MV - El Camino Café",
                    "photo": "/uploads/f7db0b70f3c84e7cba8bc3b269ed0241.jpg",
                    "id": "5e0a4fb06f894a44bbc0d238"
                },
                {
                    "businessdays": [],
                    "_id": "5e0a4fc26f894a44bbc0d239",
                    "name": "The Bistro",
                    "phoneNumber": "",
                    "createdAt": "2019-12-30T19:28:02.427Z",
                    "updatedAt": "2020-01-14T21:35:21.618Z",
                    "__v": 0,
                    "campus": "5e00fe557ecc0b000e1c0fa1",
                    "campusName": "Mountain View",
                    "desc": "The Bistro is located on the ground floor of the main hospital. Featuring gourmet coffee from Moksha Coffee Roasting, a local business focused on providing sustainable and shade grown coffee. Other offerings include fresh fruit smoothies, breakfast items, sandwiches, soup, salads, desserts and more. Vegetarian and gluten-free options are available daily. Dine in our cozy seating area or grab something to go.",
                    "fullName": "MV - The Bistro",
                    "photo": "/uploads/44e81fa1abba45f69d10e61cf63b306f.jpg",
                    "id": "5e0a4fc26f894a44bbc0d239"
                },
                {
                    "businessdays": [],
                    "_id": "5e1e344cbf265e000eb355c4",
                    "name": "Java Junction",
                    "fullName": "MV - Java Junction",
                    "desc": "Java Junction offers a wide variety of espresso and tea drinks featuring Starbucks Coffee. Pre-packaged snacks, muffins, cookies, croissants and fruit are also available.",
                    "createdAt": "2020-01-14T21:36:12.423Z",
                    "updatedAt": "2020-01-14T21:36:12.542Z",
                    "__v": 0,
                    "campus": "5e00fe557ecc0b000e1c0fa1",
                    "photo": "/uploads/37f75a8019804503b7bd24e80cb12ff6.jpg",
                    "id": "5e1e344cbf265e000eb355c4"
                }
            ],
            "photo": null,
            "id": "5e18e270147424000e337acc"
        },
        {
            "businessdays": [],
            "_id": "5e1cc774d40bf0000e83af66",
            "name": "Library",
            "moreInfoLink": "https://www.elcaminohealth.org/services/health-library-resource-center/health-library-services",
            "desc": "You can use the services at the Health Library & Resource Center, located on the main floor of the hospital, in person, over the phone or online. If you visit or call, you will be assisted by the same professional research librarians who support the information needs of physicians and nurses at the hospital.",
            "createdAt": "2020-01-13T19:39:32.894Z",
            "updatedAt": "2020-01-13T19:40:54.451Z",
            "__v": 0,
            "campus": "5e00fe557ecc0b000e1c0fa1",
            "fullName": "MV - Library",
            "children": [],
            "photo": "/uploads/432f2255abe84c46b0d2cfe736a2b9ba.jpeg",
            "id": "5e1cc774d40bf0000e83af66"
        },
        {
            "businessdays": [],
            "_id": "5e1cc7bad40bf0000e83af69",
            "name": "Gift shops",
            "hasChild": true,
            "fullName": "MV - Gift Shops",
            "createdAt": "2020-01-13T19:40:42.178Z",
            "updatedAt": "2020-01-13T19:40:42.294Z",
            "__v": 0,
            "campus": "5e00fe557ecc0b000e1c0fa1",
            "children": [
                {
                    "businessdays": [
                        {
                            "hours": [
                                {
                                    "_id": "5e1cc9a9d40bf0000e83af6c",
                                    "name": "Weekday-9-6",
                                    "startTime": "9 AM",
                                    "endTime": "6 PM",
                                    "groupName": "Weekday",
                                    "createdAt": "2020-01-13T19:48:57.988Z",
                                    "updatedAt": "2020-01-13T19:48:58.065Z",
                                    "__v": 0,
                                    "id": "5e1cc9a9d40bf0000e83af6c"
                                }
                            ],
                            "_id": "5e1cc87fd40bf0000e83af6b",
                            "name": "The Gift Connection - Weekday",
                            "daysOfWeek": "2,3,4,5,6",
                            "displayName": "Weekday",
                            "createdAt": "2020-01-13T19:43:59.089Z",
                            "updatedAt": "2020-01-13T19:50:46.506Z",
                            "__v": 0,
                            "id": "5e1cc87fd40bf0000e83af6b"
                        },
                        {
                            "hours": [
                                {
                                    "_id": "5e1cc9ded40bf0000e83af6d",
                                    "name": "Weekend-10-4",
                                    "startTime": "10 AM",
                                    "endTime": "4 PM",
                                    "groupName": "Weekend and Holiday",
                                    "createdAt": "2020-01-13T19:49:50.956Z",
                                    "updatedAt": "2020-01-13T19:49:51.034Z",
                                    "__v": 0,
                                    "id": "5e1cc9ded40bf0000e83af6d"
                                }
                            ],
                            "_id": "5e1cca40d40bf0000e83af6e",
                            "name": "The Gift Connection - Weekend",
                            "daysOfWeek": "1,7",
                            "displayName": "Weekend",
                            "createdAt": "2020-01-13T19:51:28.677Z",
                            "updatedAt": "2020-01-13T19:51:28.753Z",
                            "__v": 0,
                            "id": "5e1cca40d40bf0000e83af6e"
                        }
                    ],
                    "_id": "5e1cc83dd40bf0000e83af6a",
                    "name": "The Gift Connection",
                    "phoneNumber": "650-962-5873",
                    "desc": "The Gift Connection, located in the main hospital lobby, carries convenience items, books, newspapers, magazines, fresh floral arrangements, greeting cards and a variety of gift items. You can order a plant or floral bouquet from our gift shop over the phone with a credit card and get same-day or next-day delivery to a patient’s room at no extra cost.",
                    "createdAt": "2020-01-13T19:42:53.989Z",
                    "updatedAt": "2020-01-14T22:27:58.203Z",
                    "__v": 0,
                    "campus": "5e00fe557ecc0b000e1c0fa1",
                    "photo": "/uploads/71afd31fa1764523ad999c11ffd0b016.jpeg",
                    "id": "5e1cc83dd40bf0000e83af6a"
                }
            ],
            "photo": null,
            "id": "5e1cc7bad40bf0000e83af69"
        },
        {
            "businessdays": [],
            "_id": "5e1e45d0bf265e000eb355c9",
            "name": "Massage",
            "fullName": "MV - Massage",
            "desc": "Massage can relieve stress and pain and improve circulation and healing. As a special service to our patients, we provide free, 15-minute massages by an experienced, massage therapist. You need to receive approval from your doctor beforehand. To schedule an appointment, let your nurse know that you’re interested.",
            "createdAt": "2020-01-14T22:50:56.920Z",
            "updatedAt": "2020-01-14T22:50:57.046Z",
            "__v": 0,
            "campus": "5e00fe557ecc0b000e1c0fa1",
            "children": [],
            "photo": "/uploads/451ca56b17a94c73a1b447c4e967c50a.jpeg",
            "id": "5e1e45d0bf265e000eb355c9"
        },
        {
            "businessdays": [],
            "_id": "5e1e515bbf265e000eb355cc",
            "name": "Music & healing",
            "fullName": "MV - Music & healing",
            "phoneNumber": "650-962-5836",
            "desc": "Through the Healing Arts Program, we bring the soothing sounds of jazz, classical guitar, Celtic and double-strung harp, piano and voice to the bedside to create warmth and comfort to our hospital. Professional musicians go from ward to ward, playing in hallways and along bedsides for patients, staff and visitors.\n\nProfessional musicians who visit our campuses include:\n\nJeff Buenz, who plays Latin and jazz guitar and bass.\nBarbary Grant, who plays Celtic music and traditional songs on the Irish harp.\nDona Reyes, who plays classical Spanish guitar music.\nDavid Snellbacher, who plays an assortment of guitar music including old English, Celtic, classical and traditional Christmas carols and contemporary music.\nTo arrange a visit during your stay, call guest services at 650-962-5836.",
            "createdAt": "2020-01-14T23:40:11.206Z",
            "updatedAt": "2020-01-14T23:40:29.049Z",
            "__v": 0,
            "campus": "5e00fe557ecc0b000e1c0fa1",
            "children": [],
            "photo": "/uploads/7b12ed03a6f342caae96d6e69df8180a.jpg",
            "id": "5e1e515bbf265e000eb355cc"
        }
    ]

我收到此错误:希望对Array进行解码,但是在尝试对JSON进行解码时找到了字典。我没有看到任何字典,所以我很困惑为什么会抛出这个字典。有什么想法吗?

json swift codable
1个回答
0
投票

您是要解码单个Amenity还是它们的数组?

JSON包含它们的数组,因此您应该做类似的事情(使用[Amenity].self-注意方括号-它将解码Amenity数组:]

let amenities = try! decoder.decode([Amenity].self, from: json)

不与Amenity.self一起使用(它解码单个实例Amenity)。

© www.soinside.com 2019 - 2024. All rights reserved.