我正在尝试使用 Codable
调用 API
并且我想访问所有 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 调用中使用 Codable 和 Decodable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55792132/