我需要以jSON格式而不是Object返回的结构。我该怎么办?
对象
struct ApplePayTransactionResult: Codable {
struct TransactionResponse: Codable {
let authCode: Int?
let avsResponseCode: String?
let creditCardBin: Int?
let creditCardCountry: String?
let creditCardLast4: Int?
let creditCardScheme: String?
let cvvResponseCode: String?
...
}
let transactionResponse: TransactionResponse?
编码键
enum CodingKeys: String, CodingKey {
case transactionResponse = "transaction-response"
}
static func get(json: Any?) -> ApplePayTransactionResult? {
do {
if let data = json {
let data_ = try JSONSerialization.data(withJSONObject: data, options: []);
let decoder = JSONDecoder()
let applePayTransactionResult = try decoder.decode(ApplePayTransactionResult.self, from: data_)
return applePayTransactionResult
}else{
return nil
}
错误报告
}catch let err {
print("Err - Apple Pay Transaction Result - get", err)
return nil
}
}
}
此处返回的格式为“对象”
"{transaction-response = \ {authCode = 123456; avsResponseCode = YYY; creditCardBin = 518041; creditCardCountry = US; creditCardLast4 = 1433; creditCardScheme = Mastercard; cvvResponseCode = M; etc... ;};};
,并且需要返回纯JSON格式
{"transaction-response":{"authCode":"123456","avsResponseCode":"YYY","creditCardBin":"518041","creditCardCountry":"US","creditCardLast4":"1433","creditCardScheme":"Mastercard","cvvResponseCode":"M", etc ...}}