You need to set your decoder keyDecodingStrategy
property value to .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)
}
This will print:
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"))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…