我试图使用API调用Codable,我想访问dictionary中的所有API数组。
这可能是从codable开始的吗?
API响应例如:

{
    "status": true,
    "logo": "https://abc.png",
    "data": [
        {
            "crumb": {
                "Menu": {
                    "navigate": "Home",
                },
            },
            "path": "2",
            "type": "type0",
            "orientation": [
                {
                    "name": "All",
                }
            ],
        },
    ]
}

最佳答案

您发布的api响应是无效的json(它有一堆使其非法的尾随逗号)。这需要在生产者方面进行更改,完成此操作后,可以使用此结构访问数据:

struct Entry: Codable {
    let status: Bool
    let logo: String
    let data: [Datum]
}

struct Datum: Codable {
    let crumb: Crumb
    let path, type: String
    let orientation: [Orientation]
}

struct Crumb: Codable {
    let menu: Menu

    enum CodingKeys: String, CodingKey {
        case menu = "Menu"
    }
}

struct Menu: Codable {
    let navigate: String
}

struct Orientation: Codable {
    let name: String
}

关于ios - 在API调用中使用可编码和可解码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55792132/

10-14 23:26
查看更多