简体   繁体   English

在API调用中使用可编码和可解码

[英]Use Codable And Decodable in API Call

I'm trying to call API using Codable and i want to access all dictionary , arrays from API . 我正在尝试使用Codable调用API ,我想从API访问所有dictionary ,数组。

Is This Possible from codable ? 这可能来自可codable吗?

API Response eg: API响应,例如:

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

The API response you've posted is invalid JSON (it has a bunch of trailing commas that make it illegal). 您发布的API响应是无效的JSON(它带有很多结尾的逗号,使其成为非法)。 This needs to be changed on the producer's side, and when you've done that, you can use this struct to access the data: 这需要在生产者方面进行更改,完成此操作后,可以使用此结构访问数据:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM