ios - 如何从 Optional(Optional(<__NSSingleObjectArrayI >(25))) 中获取值
问题描述
我正在尝试获取“价格”键的值,即“25”
我从后端收到这个响应 Json
{
"errorCode": 0,
"message": "Request successfully served.",
"data": {
"games": {
"TWELVEBYTWENTYFOUR": {
"jackpot_amount": "KES 40,000.00",
"draw_date": "2021-05-21 10:59:45",
"extra": {
"jackpotAmount": 40000,
"unitCostJson": [
{
"currency": "KES",
"price": 25
}
]
},
}
},
"currentTime": {
"date": "2021-05-20 22:28:18.738038"
}
}
}
到目前为止,这是我的代码:
fetchData { (dict, error) in
let playerLoginInfo = dataDict["data"] as? NSDictionary
let playerGameInfo = playerLoginInfo?.value(forKey: "games") as? NSDictionary
if let TWELVEBYTWENTYFOUR = playerGameInfo?.value(forKey: "TWELVEBYTWENTYFOUR") as? NSDictionary {
let extra = TWELVEBYTWENTYFOUR.value(forKey: "extra") as? NSDictionary
let unitCostJson = extra?.value(forKey: "unitCostJson") as? NSArray
print("price")
print(unitCostJson?.value(forKey: "price") as? Any)
}
}
我知道这是控制台:
Optional(Optional(<__NSSingleObjectArrayI 0x600001f091d0>(
25
)
))
我见过这个问题How can I access values within Optional NSSingleObjectArrayI? 但我想不出解决办法
编辑:
我现在使用 Codeable 来获取数据:
struct Resp: Codable {
let errorCode: Int
let message: String
let data: Dat
}
struct Dat: Codable {
let games: Games
let currentTime: CurrentTime
}
struct Games: Codable {
let game_code: String
let datetime: String
let estimated_jackpot: String
let guaranteed_jackpot: String
let jackpot_title: String
let jackpot_amount: String
let draw_date: String
let extra: Extra
let next_draw_date: String
let active: String
}
struct Extra: Codable {
let currentDrawNumber: Int
let currentDrawFreezeDate: String
let currentDrawStopTime: String
let jackpotAmount: Int
let unitCostJson: [UnitCostJson]
}
struct UnitCostJson: Codable {
let currency: String
let price: Int
}
struct CurrentTime: Codable {
let date: String
let timezone_type: Int
let timezone: String
}
我现在正在尝试使用此代码从价格中获得价值
do{
let resp:Resp = try JSONDecoder().decode(Resp.self , from:data);
let data = resp.data
let games = data.games
let extra = games.extra
let unitCostJson = extra.unitCostJson
print(unitCostJson[0].price)
}
catch{
GlobalFunctions.shared.callOnMainThread {
self.showAlert(Message: "Something went wrong. Please retry.")
}
}
它正在进入catch
我现在应该如何在 unitCostJson 上获取数据?
解决方案
我屠杀了您的结构并删除了所有不相关的属性(与 json 相比),如果您想将它们添加回来,那么您需要使用 CodingKey 枚举
struct Resp: Codable {
let errorCode: Int
let message: String
let data: Dat
}
struct Dat: Codable {
let games: [String:Games]
let currentTime: CurrentTime
}
struct Games: Codable {
let extra: Extra
}
struct Extra: Codable {
let unitCostJson: [UnitCostJson]
}
struct UnitCostJson: Codable {
let currency: String
let price: Int
}
struct CurrentTime: Codable {
let date: String
}
现在您可以像这样访问 unitCost
let unitCost = resp.data.games["TWELVEBYTWENTYFOUR"]?.extra.unitCostJson
推荐阅读
- python - 通过 API 显示执行 Celery 任务的结果
- winapi - 即使返回 true,RegisterPointerInputTarget 也不执行任何操作
- html - 如何在 Safari v12 上离线运行 PWA 页面
- php - 获取 XML 变量
- bash - 如何在不使用任何外部编程语言(awk 等)的情况下打印 bash 中最常出现的单词
- python - 如何根据输入 CSV 中的条件将上一行、当前行和下一行写入输出 CSV 文件
- ruby-on-rails - 解析 youtube RSS 提要
- gradle - Gradle 构建失败 - 颤振
- java - 使用 Page Factory 时,是在自己的页面中定义相似的元素还是只定义一次更好?
- javascript - Redux 状态设置对象到对象