gpt4 book ai didi

ios - 来自 Tracker Network API (SWIFT) 的 JSON

转载 作者:行者123 更新时间:2023-11-28 05:50:40 26 4
gpt4 key购买 nike

我在从我的 API 读取数据时遇到问题。我可以在 Xcode 中看到我的 API 中的所有数据。

这是我要解析的数据。

  "data": {
"id": "2f464c52-81a5-4b2d-9d08-21baf10134bd",
"type": "player",
"metadata": {
"statsCategoryOrder": [
"levels",
"combat",
"game",
"bullets",
"ekia-enemy-killed-in-action",
"objective",
"team-play",
"extra"
],
"platformId": 1,
"platformUserHandle": "BarMexico67",
"accountId": "2f464c52-81a5-4b2d-9d08-21baf10134bd",
"cacheExpireDate": "10/31/2018 1:45:55 AM"
},
"stats": [
{
"metadata": {
"key": "level",
"name": "Level",
"categoryKey": "levels",
"categoryName": "Levels",
"isReversed": false,
"iconUrl": "https://cod-cdn.tracker.gg/assets/ranks/rank_6.png"
},
"value": 6.0,
"percentile": 92.0,
"displayValue": "6"
},
{
"metadata": {
"key": "KDRatio",
"name": "KD Ratio",
"categoryKey": "combat",
"categoryName": "Combat",
"isReversed": false
},
"value": 0.39,
"percentile": 96.0,
"displayValue": "0.39",
"displayRank": ""
},

我正在尝试获取 Level 和 KD Ratio 的值并将其放置在标签上。

但是,我收到错误“keyNotFound(CodingKeys(stringValue: "stats", intValue: nil)

这是我当前的代码。任何帮助都会很棒。

struct Data: Decodable {
let id: String?
let type: String?
let stats: [Stats]
}

struct Stats: Decodable {
let name: String?
let categoryName: String?
let displayValue: Int?
let value: Int?
}

class StatTrackerVC: UIViewController {

@IBOutlet weak var accountNameLabel: UILabel!

let blackOps4TrackerNetworkAPI = "4db06ff7-4499-494d-8d75-93a9b38292a3"
let urlString = URL(string: "https://cod-api.tracker.gg/v1/standard/bo4/profile/1/BarMexico67")


override func viewDidLoad() {
super.viewDidLoad()

let jsonUrlString = "https://cod-api.tracker.gg/v1/standard/bo4/profile/1/BarMexico67"
guard let url = URL(string: jsonUrlString) else
{ return }

var request = URLRequest(url: url)
request.httpMethod = "GET"
request.addValue(blackOps4TrackerNetworkAPI, forHTTPHeaderField: "TRN-Api-Key")


URLSession.shared.dataTask(with: request) { (data, response, err) in
//Perhaps Check Err
//Check Response Status 200 OK

guard let data = data else { return }

// let dataAsString = String(data: data, encoding: .utf8)
// print(dataAsString!)

do {
let stats = try JSONDecoder().decode(Data.self, from: data)
print(stats)

} catch let jsonErr {
print("Error Serializing JSON:", jsonErr)
}
}.resume()

}

最佳答案

实际上 stats 对象不在 data 对象中,而是它们处于同一级别。所以,stats 数组不应该在 Data 类型中。同样在 stat 对象中,metaData 是一个子对象,它具有 keycategoryKeycategoryName 等键,而 valuepercentiledisplayValuestat 对象的一部分。可解码类型的正确实现如下,

struct TrackerResponse: Decodable {
let data: StatData?
let stats: [Stat]?
}

struct StatData: Decodable {
let id: String?
let type: String?
let accountId: String?
let platformId: Int?
}


struct Stat: Decodable {
let value: Int?
let metadata: StatMeta?
let percentile: Int?
let displayValue: String?
}

struct StatMeta: Decodable {
let key: String?
let name: String?
let categoryKey: String?
let categoryName: String?
}

所以,现在你应该解码 TrackerResponse,如下所示,

do {
print(String(data: data, encoding: .utf8))
let response = try JSONDecoder().decode(TrackerResponse.self, from: data)
print(response.data?.id)

} catch let jsonErr {
print("Error Serializing JSON:", jsonErr)
}

关于ios - 来自 Tracker Network API (SWIFT) 的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53075777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com