gpt4 book ai didi

ios - 如何在 Swift 中从加密的 JSON 字符串中提取某些字段?

转载 作者:行者123 更新时间:2023-11-30 13:07:48 24 4
gpt4 key购买 nike

我正在将 Apple Pay 集成到我的应用程序中。我有一个用 Java 编写的服务,它实际上获取从我的 iOS 应用程序发送的加密 blob,并将其解密并将其发回。

我在这里面临两个问题:

  1. 更大的问题 - 我的服务仅消耗 XML 负载,因此我需要在我的 iOS 应用程序中将 JSON 负载转换为 XML 负载我不知道该怎么办。
  2. 较小的问题 - 我正在尝试从这个加密的 JSON 负载中提取字段并将其打印在控制台上。

示例:

let jsonBody = payment.token.paymentData
print("jsonBody = \(jsonBody)") !!! WORKS (prints encrypted data) !!!

但是,我无法按照以下链接提取 paymentData 字典中存在的其他参数 https://developer.apple.com/library/ios/documentation/PassKit/Reference/PaymentTokenJSON/PaymentTokenJSON.html

示例:

let paymentDataDict:[String:String] = payment.token.paymentData
for (key, value) in paymentDataDict {
print("Dictionary key \(key) - Dictionary value \(value)")
}

Error: Cannot convert value of type 'NSData' to specified type '[String : String]'

来源:

 /*
* This is the first method to be called by the passkit framework once the encrypted blob is received from the apple server.
* Our implementation of the call back function below is calling payment service to decrypt the encrypted blob.
* It then proceeds to call processPayment method to send this data down to payment service for authorization.
*/
func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: (PKPaymentAuthorizationStatus) -> Void) {
let decryptUrl = NSURL(string: "http://lvsdevwsv04-01.us.gspt.net:1801/public-api-service/v1.0/stores/TMSUS/payments/decryptblob.xml") // Decryption URL
let decryptRequest = NSMutableURLRequest(URL: decryptUrl!)
decryptRequest.HTTPMethod = "POST"
decryptRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
decryptRequest.setValue("application/json", forHTTPHeaderField: "Accept")
let jsonBody = payment.token.paymentData
print("jsonBody = \(jsonBody)")

//ERROR IN THE BELOW LINE
let paymentDataDict:[String:String] = payment.token.paymentData
for (key, value) in paymentDataDict {
print("Dictionary key \(key) - Dictionary value \(value)")
}

decryptRequest.HTTPBody = jsonBody
let task = NSURLSession.sharedSession().dataTaskWithRequest(decryptRequest) {data, response, error in
guard error == nil && data != nil else {
print("error=\(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {
print("statuscode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
} else {
let responseString = NSString(data: data!, en

//code removed for brevity

最佳答案

将加密的字符串转换为 NSString 对象后,我终于能够在控制台上打印它了。这是经过一些细微更改后对我有用的代码片段。

    let encryptedPaymentData = payment.token.paymentData
let paymentMethod = payment.token.paymentMethod
let txId = payment.token.transactionIdentifier
print("paymentMethod = \(paymentMethod)")
print("txId = \(txId)")
let decryptedPaymentData:NSString! = NSString(data: encryptedPaymentData, encoding: NSUTF8StringEncoding)
print("decryptedPaymentData = \(decryptedPaymentData)")

关于ios - 如何在 Swift 中从加密的 JSON 字符串中提取某些字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39130933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com