我如何在 Swift 中解码此 JSON 4
How do I Decode this JSON in Swift 4
我有这个 JSON 响应需要解码,但我只需要它的某些属性。我想获取传奇的名字和他们对传奇的杀戮数量。我如何通过图例模型构造以使解码器正常工作?
`
"legends": {
"selected": {
"LegendName": "Pathfinder",
"data": [
{
"name": "Season 5 Kills",
"value": 757,
"key": "kills_season_5"
},
{
"name": "Kills",
"value": 1140,
"key": "kills"
}
],
"ImgAssets": {
"icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/pathfinder.png",
"banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/pathfinder.jpg"
}
},
"all": {
"Pathfinder": {
"data": [
{
"name": "Kills",
"value": 1140,
"key": "kills"
},
{
"name": "Damage",
"value": 257532,
"key": "damage"
},
{
"name": "Season 5 Kills",
"value": 757,
"key": "kills_season_5"
}
],
"ImgAssets": {
"icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/pathfinder.png",
"banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/pathfinder.jpg"
}
},
"Wraith": {
"data": [
{
"name": "Kills",
"value": 269,
"key": "kills"
},
{
"name": "Season 5 Wins",
"value": 7,
"key": "wins_season_5"
},
{
"name": "Season 5 Kills",
"value": 174,
"key": "kills_season_5"
}
],
"ImgAssets": {
"icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/wraith.png",
"banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/wraith.jpg"
}
},
"Octane": {
"data": [
{
"name": "Season 5 Kills",
"value": 106,
"key": "kills_season_5"
}
],
"ImgAssets": {
"icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/octane.png",
"banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/octane.jpg"
}
},
"Wattson": {
"ImgAssets": {
"icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/wattson.png",
"banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/wattson.jpg"
}
}
}
},
}
以上是 JSON 响应,我希望解码器仅显示其数据数组中的传说以及击杀和伤害。我目前无法访问每个图例下的“数据”数组。
您可以使用 Codable
来获得预期的响应。使用以下模型,
struct Response: Codable {
let legends: Legends
}
struct Legends: Codable {
let all: [String:Legend]
}
struct Legend: Codable {
let data: [LegendData]?
var kills: Int {
return self.data?.filter({[=10=].key.lowercased() == "kills"}).first?.value ?? 0
}
var damage: Int {
return self.data?.filter({[=10=].key.lowercased() == "damage"}).first?.value ?? 0
}
}
struct LegendData: Codable {
let value: Int
let key: String
}
由于每个 Legend
只需要 kills
和 damage
的计数,因此无需解析 JSON 中的所有内容.
在 Legend
模型中,我创建了 2 个 计算属性 kills
和 damage
。这些属性将直接 return 每个图例的击杀数和伤害数,而无需在每次访问时都编写那么多代码。
现在解析 JSON data
像这样,
do {
let response = try JSONDecoder().decode(Response.self, from: data)
response.legends.all.forEach { (name,legend) in
let kills = legend.kills
let damage = legend.damage
print("\(name) - Kills: \(kills), Damage: \(damage)")
}
} catch {
print(error)
}
示例输出:
Pathfinder - Kills: 1140, Damage: 257532
Octane - Kills: 0, Damage: 0
Wattson - Kills: 0, Damage: 0
Wraith - Kills: 269, Damage: 0
我有这个 JSON 响应需要解码,但我只需要它的某些属性。我想获取传奇的名字和他们对传奇的杀戮数量。我如何通过图例模型构造以使解码器正常工作?
`
"legends": {
"selected": {
"LegendName": "Pathfinder",
"data": [
{
"name": "Season 5 Kills",
"value": 757,
"key": "kills_season_5"
},
{
"name": "Kills",
"value": 1140,
"key": "kills"
}
],
"ImgAssets": {
"icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/pathfinder.png",
"banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/pathfinder.jpg"
}
},
"all": {
"Pathfinder": {
"data": [
{
"name": "Kills",
"value": 1140,
"key": "kills"
},
{
"name": "Damage",
"value": 257532,
"key": "damage"
},
{
"name": "Season 5 Kills",
"value": 757,
"key": "kills_season_5"
}
],
"ImgAssets": {
"icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/pathfinder.png",
"banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/pathfinder.jpg"
}
},
"Wraith": {
"data": [
{
"name": "Kills",
"value": 269,
"key": "kills"
},
{
"name": "Season 5 Wins",
"value": 7,
"key": "wins_season_5"
},
{
"name": "Season 5 Kills",
"value": 174,
"key": "kills_season_5"
}
],
"ImgAssets": {
"icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/wraith.png",
"banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/wraith.jpg"
}
},
"Octane": {
"data": [
{
"name": "Season 5 Kills",
"value": 106,
"key": "kills_season_5"
}
],
"ImgAssets": {
"icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/octane.png",
"banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/octane.jpg"
}
},
"Wattson": {
"ImgAssets": {
"icon": "https:\/\/api.mozambiquehe.re\/assets\/icons\/wattson.png",
"banner": "https:\/\/api.mozambiquehe.re\/assets\/banners\/wattson.jpg"
}
}
}
},
}
以上是 JSON 响应,我希望解码器仅显示其数据数组中的传说以及击杀和伤害。我目前无法访问每个图例下的“数据”数组。
您可以使用 Codable
来获得预期的响应。使用以下模型,
struct Response: Codable {
let legends: Legends
}
struct Legends: Codable {
let all: [String:Legend]
}
struct Legend: Codable {
let data: [LegendData]?
var kills: Int {
return self.data?.filter({[=10=].key.lowercased() == "kills"}).first?.value ?? 0
}
var damage: Int {
return self.data?.filter({[=10=].key.lowercased() == "damage"}).first?.value ?? 0
}
}
struct LegendData: Codable {
let value: Int
let key: String
}
由于每个 Legend
只需要 kills
和 damage
的计数,因此无需解析 JSON 中的所有内容.
在 Legend
模型中,我创建了 2 个 计算属性 kills
和 damage
。这些属性将直接 return 每个图例的击杀数和伤害数,而无需在每次访问时都编写那么多代码。
现在解析 JSON data
像这样,
do {
let response = try JSONDecoder().decode(Response.self, from: data)
response.legends.all.forEach { (name,legend) in
let kills = legend.kills
let damage = legend.damage
print("\(name) - Kills: \(kills), Damage: \(damage)")
}
} catch {
print(error)
}
示例输出:
Pathfinder - Kills: 1140, Damage: 257532
Octane - Kills: 0, Damage: 0
Wattson - Kills: 0, Damage: 0
Wraith - Kills: 269, Damage: 0