gpt4 book ai didi

json - 如何使用 SwiftyJSON 解析带有 AnyHashable 值的 PayPal JSON 响应?

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

我正在使用 PaypalSDK 将 paypal 支付方式添加到我正在开发的应用程序中,它已经在运行,当支付成功时,我收到了一个响应,我正在将其转换为 jsonObject 但我不知道如何解析它以便仅从响应中提取代码。这是我得到的回应

JSON: [AnyHashable("response"): {
code = "******************* -****************-**********************";
}, AnyHashable("response_type"): authorization_code, AnyHashable("client"): {
environment = sandbox;
"paypal_sdk_version" = "2.11.5";
platform = iOS;
"product_name" = "PayPal iOS SDK";
}]

这就是我的 payPalFuturePaymentViewController 方法:

  func payPalFuturePaymentViewController(_ futurePaymentViewController: PayPalFuturePaymentViewController, didAuthorizeFuturePayment futurePaymentAuthorization: [AnyHashable: Any]) {
print("PayPal Future Payment Authorization Success!")
self.resultText = futurePaymentAuthorization.description
let jsonObject = JSON(futurePaymentAuthorization.description)
print("JSON: \(jsonObject)")
// send authorization to your server to get refresh token.
futurePaymentViewController.dismiss(animated: true, completion: { () -> Void in

var paypalPago = PagoItem(noTarjeta: "", fechaExp: "", cvc: "", token: "", tipo: "PayPal")

self.metodosPago.append(paypalPago)
self.saveMetodo()

let destViewController : UIViewController = self.storyboard!.instantiateViewController(withIdentifier: "pagosLlenos")
var vcArray = self.navigationController?.viewControllers

vcArray?.removeLast()
vcArray?.append(destViewController)
self.navigationController?.setViewControllers(vcArray!, animated: true)
})
}

所以我想做的是从响应中获取代码,将其放入一个变量中,然后将该变量包含在 paypalPago 项目中:

 var paypalPago = PagoItem(noTarjeta: "", fechaExp: "", cvc: "", token: PayPalCode, tipo: "PayPal")

任何有关如何解析此 json 并提取我需要的代码的帮助将不胜感激:)

最佳答案

已解决

其实很简单,问题是我存储的是字符串描述而不是实际的 JSON 响应,所以我不得不更改

let jsonObject = JSON(futurePaymentAuthorization.description)

let jsonObject = JSON(futurePaymentAuthorization)

现在我的回复是这样的:

JSON: {
"client" : {
"environment" : "sandbox",
"product_name" : "PayPal iOS SDK",
"paypal_sdk_version" : "2.11.5",
"platform" : "iOS"
},
"response_type" : "authorization_code",
"response" : {
"code" : "****************_*********_***********************"
}

现在我可以像解析任何普通 JSON 一样解析它

    let response = jsonObject["response"]["code"].string!
print(response)

// send authorization to your server to get refresh token.
futurePaymentViewController.dismiss(animated: true, completion: { () -> Void in
var paypalPago = PagoItem(noTarjeta: "", fechaExp: "", cvc: "", token: response, tipo: "PayPal")
self.metodosPago.append(paypalPago)

关于json - 如何使用 SwiftyJSON 解析带有 AnyHashable 值的 PayPal JSON 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42496320/

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