在 Swift 中解码 API 响应
Decoding an API response in Swift
我正在使用 Alamofire 解码对象,我的 JSON 看起来像这样
{
"wallet": {
"public_key": "ADDRESS",
"name": "My test wallet",
"id": "-MQ9NdAyMaK3WQfSOYZW",
"owner": "Kca8BNHy8FemIxPO7FWBSLE8XKN2",
"currency": "ME",
"metadata": {
"legacy_address": "WHAT",
"x_pub_address": "SOMETHING COOL"
}
},
"secrets": {
"wif": "SOMETHING",
"private_key": "SOMETHING ELSE",
"mnemonic": "YOU WISH"
}
}
我的 Swift 看起来像这样:
class Response: Codable {
var wallet: Wallet
var secrets: [String: String]
}
class Wallet: Codable {
var publicKey: String
var name: String
var id: String
var owner: String
var currency: String
var metadata: [String: String]
enum WalletKeys: String, CodingKey{
case publicKey = "public_key",
case name,
case id
case owner,
case currency,
case metadata
}
}
我收到 keyNotFound(CodingKeys(stringValue: "publicKey"))
错误,我不知道为什么。有人可以帮我解决这个问题吗?
您需要将解码器 keyDecodingStrategy
属性 值设置为 .convertFromSnakeCase
:
struct Response: Codable {
let wallet: Wallet
let secrets: Secrets
}
struct Secrets: Codable {
let wif: String
let privateKey: String
let mnemonic: String
}
struct Wallet: Codable {
let publicKey: String
let name: String
let id: String
let owner: String
let currency: String
let metadata: Metadata
}
struct Metadata: Codable {
let legacyAddress: String
let xPubAddress: String
}
let json = """
{
"wallet": {
"public_key": "ADDRESS",
"name": "My test wallet",
"id": "-MQ9NdAyMaK3WQfSOYZW",
"owner": "Kca8BNHy8FemIxPO7FWBSLE8XKN2",
"currency": "ME",
"metadata": {
"legacy_address": "WHAT",
"x_pub_address": "SOMETHING COOL"
}
},
"secrets": {
"wif": "SOMETHING",
"private_key": "SOMETHING ELSE",
"mnemonic": "YOU WISH"
}
}
"""
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let root = try decoder.decode(Response.self, from: Data(json.utf8))
print(root)
} catch {
print(error)
}
这将打印:
Response(wallet: Wallet(publicKey: "ADDRESS", name: "My test wallet", id: "-MQ9NdAyMaK3WQfSOYZW", owner: "Kca8BNHy8FemIxPO7FWBSLE8XKN2", currency: "ME", metadata: Metadata(legacyAddress: "WHAT", xPubAddress: "SOMETHING COOL")), secrets: __lldb_expr_1.Secrets(wif: "SOMETHING", privateKey: "SOMETHING ELSE", mnemonic: "YOU WISH"))
我正在使用 Alamofire 解码对象,我的 JSON 看起来像这样
{
"wallet": {
"public_key": "ADDRESS",
"name": "My test wallet",
"id": "-MQ9NdAyMaK3WQfSOYZW",
"owner": "Kca8BNHy8FemIxPO7FWBSLE8XKN2",
"currency": "ME",
"metadata": {
"legacy_address": "WHAT",
"x_pub_address": "SOMETHING COOL"
}
},
"secrets": {
"wif": "SOMETHING",
"private_key": "SOMETHING ELSE",
"mnemonic": "YOU WISH"
}
}
我的 Swift 看起来像这样:
class Response: Codable {
var wallet: Wallet
var secrets: [String: String]
}
class Wallet: Codable {
var publicKey: String
var name: String
var id: String
var owner: String
var currency: String
var metadata: [String: String]
enum WalletKeys: String, CodingKey{
case publicKey = "public_key",
case name,
case id
case owner,
case currency,
case metadata
}
}
我收到 keyNotFound(CodingKeys(stringValue: "publicKey"))
错误,我不知道为什么。有人可以帮我解决这个问题吗?
您需要将解码器 keyDecodingStrategy
属性 值设置为 .convertFromSnakeCase
:
struct Response: Codable {
let wallet: Wallet
let secrets: Secrets
}
struct Secrets: Codable {
let wif: String
let privateKey: String
let mnemonic: String
}
struct Wallet: Codable {
let publicKey: String
let name: String
let id: String
let owner: String
let currency: String
let metadata: Metadata
}
struct Metadata: Codable {
let legacyAddress: String
let xPubAddress: String
}
let json = """
{
"wallet": {
"public_key": "ADDRESS",
"name": "My test wallet",
"id": "-MQ9NdAyMaK3WQfSOYZW",
"owner": "Kca8BNHy8FemIxPO7FWBSLE8XKN2",
"currency": "ME",
"metadata": {
"legacy_address": "WHAT",
"x_pub_address": "SOMETHING COOL"
}
},
"secrets": {
"wif": "SOMETHING",
"private_key": "SOMETHING ELSE",
"mnemonic": "YOU WISH"
}
}
"""
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let root = try decoder.decode(Response.self, from: Data(json.utf8))
print(root)
} catch {
print(error)
}
这将打印:
Response(wallet: Wallet(publicKey: "ADDRESS", name: "My test wallet", id: "-MQ9NdAyMaK3WQfSOYZW", owner: "Kca8BNHy8FemIxPO7FWBSLE8XKN2", currency: "ME", metadata: Metadata(legacyAddress: "WHAT", xPubAddress: "SOMETHING COOL")), secrets: __lldb_expr_1.Secrets(wif: "SOMETHING", privateKey: "SOMETHING ELSE", mnemonic: "YOU WISH"))