How to parse this PayPal JSON response with AnyHashable value using SwiftyJSON?

时光毁灭记忆、已成空白 提交于 2019-12-11 04:45:35

问题


I'm using PaypalSDK to add paypal payment methods to the app I'm developing, it's already working and when the payment is succesful I'm getting a response which I'm converting into a jsonObject but I don't know how to parse it in order to extract just the code from the response. This is the response I'm getting

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";
}]

And this is what I have on my payPalFuturePaymentViewController method:

  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)
    })
}

So what I would like to do is to get the code from the response, put it in a variable and then include that variable in the paypalPago item:

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

Any help on how to parse this json and extract the code I need would be much appreciated :)


回答1:


Solved

It was very easy actually the problem was that I was storing the string description not the actual JSON response, so I had to change

let jsonObject = JSON(futurePaymentAuthorization.description)

to

let jsonObject = JSON(futurePaymentAuthorization)

Now my response looks like this:

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

And now I can parse it like any normal 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)


来源:https://stackoverflow.com/questions/42496320/how-to-parse-this-paypal-json-response-with-anyhashable-value-using-swiftyjson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!