首页 > 解决方案 > 如何从 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 上获取数据?

标签: iosjsonswift

解决方案


我屠杀了您的结构并删除了所有不相关的属性(与 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

推荐阅读