Swift:iOS Paypal响应解析为NSDictionary / NSArray

时间:2015-07-13 18:06:33

标签: json xcode swift nsarray nsdictionary

我使用PayPal sdk和Xcode for iOS

我成功地使用了它。现在,我从服务器得到以下响应:

代码:

self.resultText = completedPayment.confirmation
println(self.resultText)

结果:

[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

3 个答案:

答案 0 :(得分:4)

我刚刚用这种方式解决了......

static.

如果有人有更好的解决方案,我们分享吧......

谢谢

答案 1 :(得分:2)

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

答案 2 :(得分:0)

你可以尝试像这样使用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