ios - 在 API 调用中使用 Codable 和 Decodable
问题描述
我正在尝试调用API
usingCodable
并且我想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
}
推荐阅读
- node.js - 在设置 Stripe Webhook 签名时,我们应该拒绝未签名的事件吗?
- python - 如何存储整个迭代结果?
- android - 如何通过单击按钮动态增加布局的高度和宽度参数?
- mysql - MYSQL 数组变量(无存储过程,无临时表)
- apache-kafka - Debezium:RestClientException:领导者未知;错误代码:50004
- python - 转置 Pandas 数据框 Python
- solidity - 安全帽 balanceOf 分叉网络地址返回 0
- java - IntelliJ java自动完成完全坏了
- amazon-web-services - s3 存储桶策略访问对象 url
- python - 如何在 PyQt 中绘制自定义椭圆形状?