我在将 HTML 脚本解码为来自 JSON 的字符串值时遇到问题,其他所有值都可以正常转换

问题描述

我正在处理一个 JSON 响应,其中键的值包含 HTML 作为字符串,它不是很大,但仍然没有解码。这是我的 JSON 响应

{
    "authentication": {
        "3ds1": {
            "veResEnrolled": "Y"
        },"payerInteraction": "required","redirect": {
            "domainName": "www.abc.com"
        },"redirectHtml": "<div id=\"abcabcabc\" xmlns=\"xyzxyz"> <iframe id=\"redirectTo3ds1Frame\" name=\"redirectTo3ds1Frame\" height=\"100%\" width=\"100%\" > </iframe> <form id =\"xyz123\" method=\"POST\" action=\"https://domain.name" target=\"redirectTo3ds1Frame\"> <input type=\"hidden\" name=\"PaReq\" value=\"Some text" /> <input type=\"hidden\" name=\"TermUrl\" value=\"https:/url" /> <input type=\"hidden\" name=\"MD\" value=\"\" /> </form> <script id=\"authenticate-payer-script\"> var e=document.getElementById(\"someID\"); if (e) { e.submit(); if (e.parentNode !== null) { e.parentNode.removeChild(e); } } </script> </div>","version": "3DS1"
    },"correlationId": "testxyz123450","device": {
        "browser": "MOZILLA","ipAddress": "127.0.0.1"
    },"merchant": "ABC123","response": {
        "gatewayCode": "PENDING","gatewayRecommendation": "PROCEED"
    },"result": "PENDING","timeOfLastUpdate": "2021-07-25T06:11:09.511Z","timeOfRecord": "2021-07-25T06:10:51.208Z",}

这是我正在使用的模型

// MODEL CODE

import Foundation

// MARK: - AuthPayer
class AuthPayer: Codable {
    var timeOfLastUpdate,timeOfRecord: String?
    var device: Device?
    var merchant: String?
    var authentication: Authentication?
    var result: String?
    var correlationID: String?
    var response: Response?

    enum CodingKeys: String,CodingKey {
        case timeOfLastUpdate,timeOfRecord,device,merchant,authentication,result,transaction
        case correlationID
        case response
    }

    init( timeOfLastUpdate: String?,timeOfRecord: String?,device: Device?,merchant: String?,authentication: Authentication?,result: String?,transaction: Transaction?,correlationID: String?,response: Response?) {
        self.timeOfLastUpdate = timeOfLastUpdate
        self.timeOfRecord = timeOfRecord
        self.device = device
        self.merchant = merchant
        self.authentication = authentication
        self.result = result
        self.correlationID = correlationID
        self.response = response
    }
}

// MARK: - Authentication
class Authentication: Codable {
    var redirect: Redirect?
    var redirectHTML: String?
    var the3Ds1: The3Ds1?
    var payerInteraction,version: String?

    enum CodingKeys: String,CodingKey {
        case redirect
        case redirectHTML
        case the3Ds1
        case payerInteraction,version
    }

    init(redirect: Redirect?,redirectHTML: String?,the3Ds1: The3Ds1?,payerInteraction: String?,version: String?) {
        self.redirect = redirect
        self.redirectHTML = redirectHTML ?? """
            """
        self.the3Ds1 = the3Ds1
        self.payerInteraction = payerInteraction
        self.version = version
    }
}

// MARK: - Redirect
class Redirect: Codable {
    var domainName: String?

    init(domainName: String?) {
        self.domainName = domainName
    }
}

// MARK: - The3Ds1
class The3Ds1: Codable {
    var veResEnrolled: String?

    init(veResEnrolled: String?) {
        self.veResEnrolled = veResEnrolled
    }
}

// MARK: - Device
class Device: Codable {
    var browser,ipAddress: String?

    init(browser: String?,ipAddress: String?) {
        self.browser = browser
        self.ipAddress = ipAddress
    }
}

// MARK: - ValueTransfer
class ValueTransfer: Codable {
    var accountType: String?

    init(accountType: String?) {
        self.accountType = accountType
    }
}

// MARK: - Response
class Response: Codable {
    var gatewayRecommendation,gatewayCode: String?

    init(gatewayRecommendation: String?,gatewayCode: String?) {
        self.gatewayRecommendation = gatewayRecommendation
        self.gatewayCode = gatewayCode
    }
}

// MARK: - Provided
class Provided: Codable {
    var card: Card?

    init(card: Card?) {
        self.card = card
    }
}






// MARK: - Acquirer
class Acquirer: Codable {
    var merchantID: String?

    enum CodingKeys: String,CodingKey {
        case merchantID
    }

    init(merchantID: String?) {
        self.merchantID = merchantID
    }
}

通过使用这种方法我正在解码

let authPayer = try? decoder.decode(AuthPayer.self,from: theJSONData)

从 authPayer 我可以访问 authPayer?.authentication?.redirect?.domainName 和其他所有属性,但 authPayer?.authentication?.redirectHTML 没有得到任何值。我已经在日志中打印出响应,它确实具有包括“redirectHtml”在内的所有值

我尝试过使用多行字符串文字来存储值,但是,我无法找出仅针对“redirectHtml”的问题,我需要来自 authPayer 的 redirectHtml 的 vqlue。请帮帮我

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)