1

我正在使用 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 并提取我需要的代码的帮助将不胜感激:)

4

1 回答 1

1

解决了

实际上很简单,问题是我存储的是字符串描述而不是实际的 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)
于 2017-02-28T00:44:46.077 回答