从字典值swift中删除\n

w8rqjzmb  于 2023-05-16  发布在  Swift
关注(0)|答案(1)|浏览(229)

我有字典[String:字符串],键“header”的值与其他值相同。我想去掉这个符号“\n”,“”

"header": "{\n ephemeralPublicKey = \"xxxxx\";\n    publicKeyHash = \"xxxx\";\n    transactionId = xxxx;\n}", "version": "EC_v1", "signature": "xxxxxxx"

这个“标题”是我字典里的一个关键字。

["header": "{\n    ephemeralPublicKey = \"xxx\";\n    publicKeyHash = \"xxx\";\n    transactionId = xxx;\n}", "version": "xxx", "signature": "xxx", "data": "xxx"]

我还需要的头不是一个字符串,但要{ }
所以,我结构如下:

["header": "{\n    ephemeralPublicKey = \"xxx\";\n    publicKeyHash = \"xxx\";\n    transactionId = xxx;\n}", "version": "xxx", "signature": "xxx", "data": "xxx"]

我想撒谎:

{
      "data":"xxx",
      "signature":"xxx",
      "header":{
         "publicKeyHash":"xxx",
         "ephemeralPublicKey":"xxx",
         "transactionId":"xxx"
      },
      "version":"xxx"
   }

我明白

let data = payment.token.paymentData

接下来我做:

guard let token = try! JSONSerialization.jsonObject(with: data, options: []) as? [String : Any] else {
            return
        }

拿着这个

elements
      ▿ 0 : 2 elements
        - key : "version"
        - value : xxxxx
      ▿ 1 : 2 elements
        - key : "signature"
        - value : xxxxxxxx
      ▿ 2 : 2 elements
        - key : "data"
        - value : xxx
      ▿ 3 : 2 elements
        - key : "header"
        ▿ value : 3 elements
          ▿ 0 : 2 elements
            - key : publicKeyHash
            - value : xxx
          ▿ 1 : 2 elements
            - key : ephemeralPublicKey
            - value : xxx
          ▿ 2 : 2 elements
            - key : transactionId
            - value : xxxx

但我需要这个结构:

{
   "paymentData":{
      "data":"xxxx",
      "signature":"xxxxx",
      "header":{
         "publicKeyHash":"xxx",
         "ephemeralPublicKey":"xxx",
         "transactionId":"xxx"
      },
      "version":"xxx"
   }

下次我这样做:

let jsonData = try! JSONSerialization.data(withJSONObject: token, options: [])

        guard let string = String(data: jsonData, encoding: String.Encoding.utf8)?.replacingOccurrences(of: "\\/", with: "/").replacingOccurrences(of: "\"", with: "") else { return }

之后我的价值观 不带“”引号。

4ioopgfo

4ioopgfo1#

冒着误解你在这里试图实现的目的的风险......听起来你感兴趣的是使用“标准”Codable Swift对象将Data转换为Swift对象。
为此:

看起来是这样的

创建可解码的Swift结构体

如果这是你的JSON结构:

{
    "paymentData": {
        "data": "longdatastringhere",
        "signature": "alsolongstring",
        "header": {
            "publicKeyHash": "Ibq...",
            "ephemeralPublicKey": "MFK....",
            "transactionId": "2779..."
        },
        "version": "EC_v1"
    }
}

您可以为每个JSON对象创建Swift结构体,如下所示

struct PaymentDataContainer: Decodable {
    let paymentData: PaymentData
}

PaymentData是另一个需要创建的Swift结构体

struct PaymentData: Decodable {
    let data: String
    let signature: String
    let header: PaymentHeader
    let version: String
}

PaymentHeader是……是的,你猜对了……需要创建的Swift结构

struct PaymentHeader: Decodable {
    let publicKeyHash: String
    let ephemeralPublicKey: String
    let transactionId: String
}

使用JSONDecoder对数据进行解码

假设你在这里提到的JSON数据:
let data = payment.token.paymentData
是原始的Data,你可以像这样解码它

let jsonDecoder = JSONDecoder()
do {
    let paymentDataContainer = try jsonDecoder.decode(PaymentDataContainer.self, from: data)
    // You now have a Swift struct you can work with as normal
    print(paymentDataContainer.paymentData.version) // prints "EC_v1"
} catch {
    print(error)
}

相关问题