Json Swift:iOS Paypal响应解析到NSDictionary/NSArray
我正在使用PayPal sdk和iOS的Xcode 我成功地处理了它。现在,我从服务器得到以下响应: 代码: 结果:Json Swift:iOS Paypal响应解析到NSDictionary/NSArray,json,xcode,swift,nsarray,nsdictionary,Json,Xcode,Swift,Nsarray,Nsdictionary,我正在使用PayPal sdk和iOS的Xcode 我成功地处理了它。现在,我从服务器得到以下响应: 代码: 结果: [response: { "create_time" = "2015-07-13T17:52:31Z"; id = "PAY-NONETWORKPAYIDEXAMPLE123"; intent = sale; state = approved; }, client: { environment = mock; "paypal_sdk_version" = "2.11.1";
[response: {
"create_time" = "2015-07-13T17:52:31Z";
id = "PAY-NONETWORKPAYIDEXAMPLE123";
intent = sale;
state = approved;
},
client: {
environment = mock;
"paypal_sdk_version" = "2.11.1";
platform = iOS;
"product_name" = "PayPal iOS SDK";
},
response_type: payment]
我的问题是,我只想将所有数据结果解析并访问到NSDictionary和/或NSArray中,我就是这样解决的
let paymentResultDic = completedPayment.confirmation as NSDictionary
let dicResponse: AnyObject? = paymentResultDic.objectForKey("response")
println(dicResponse!.objectForKey("create_time"))
println(dicResponse!.objectForKey("id"))
println(dicResponse!.objectForKey("intent"))
println(dicResponse!.objectForKey("state"))
let dicClient: AnyObject? = paymentResultDic.objectForKey("client")
println(dicClient!.objectForKey("environment"))
println(dicClient!.objectForKey("paypal_sdk_version"))
println(dicClient!.objectForKey("platform"))
println(dicClient!.objectForKey("product_name"))
println(paymentResultDic.objectForKey("response_type"))
如果有人有更好的解决方案,让我们分享它
谢谢你,斯威夫特3
let paymentResultDic = completedPayment.confirmation as NSDictionary
let dicResponse: AnyObject? = paymentResultDic.object(forKey: "response") as AnyObject?
let paycreatetime:String = dicResponse!["create_time"] as! String
let payauid:String = dicResponse!["id"] as! String
let paystate:String = dicResponse!["state"] as! String
let payintent:String = dicResponse!["intent"] as! String
print("id is --->%@",payauid)
print("created time ---%@",paycreatetime)
print("paystate is ----->%@",paystate)
print("payintent is ----->%@",payintent)
您可以尝试像这样使用Pod
SwiftyJSON
:
let data = payment.confirmation as NSDictionary
var jsonObj = JSON(data)
// For the response part
let response = jsonObj["response"]
let id = response["id"].stringValue
let intent = response["intent"].stringValue
let create_time = response["create_time"].stringValue
let state = response["state"].stringValue
// For the client part
let client = jsonObj["client"]
let environment = client["environment"].stringValue
let paypal_sdk_version = client["paypal_sdk_version"].stringValue
let platform = client["platform"].stringValue
let product_name = client["product_name"].stringValue
// For the response type
let response_type = jsonObj["response_type"].stringValue
@KLD,你想从应答和clinet中获取所有数据吗?
let data = payment.confirmation as NSDictionary
var jsonObj = JSON(data)
// For the response part
let response = jsonObj["response"]
let id = response["id"].stringValue
let intent = response["intent"].stringValue
let create_time = response["create_time"].stringValue
let state = response["state"].stringValue
// For the client part
let client = jsonObj["client"]
let environment = client["environment"].stringValue
let paypal_sdk_version = client["paypal_sdk_version"].stringValue
let platform = client["platform"].stringValue
let product_name = client["product_name"].stringValue
// For the response type
let response_type = jsonObj["response_type"].stringValue