问题
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