Swift IAP稍后从json响应中获取latest_receipt_info值

时间:2016-02-16 12:55:34

标签: ios json swift in-app-purchase

你好我有工作的IAP代码,后来我得到了成功jsonResponse我希望用字符串输出得到一些值。

Json的回应是

"latest_receipt_info" =     (
            {
        "expires_date" = "2016-01-15 17:22:53 Etc/GMT";
        "expires_date_ms" = 1452878573000;
        "expires_date_pst" = "2016-01-15 09:22:53 America/Los_Angeles";
        "is_trial_period" = true;
        "original_purchase_date" = "2016-01-15 17:17:54 Etc/GMT";
        "original_purchase_date_ms" = 1452878274000;
        "original_purchase_date_pst" = "2016-01-15 09:17:54 America/Los_Angeles";
        "original_transaction_id" = 1000000189163619;
        "product_id" = "com.try.Free";
        "purchase_date" = "2016-01-15 17:17:53 Etc/GMT";
        "purchase_date_ms" = 1452878273000;
        "purchase_date_pst" = "2016-01-15 09:17:53 America/Los_Angeles";
        quantity = 1;
        "transaction_id" = 1000000189163619;
        "web_order_line_item_id" = 1000000031509205;
    },

我的Swift代码

        var jsonResponse: NSMutableDictionary?
                                do{
                                    jsonResponse = try NSJSONSerialization.JSONObjectWithData(data!,
                                        options: NSJSONReadingOptions.AllowFragments) as? NSMutableDictionary;

print(jsonResponse)

我想将 product_id , purchase_date,transaction_id,original_transaction_id字符串一起使用。

谢谢

1 个答案:

答案 0 :(得分:0)

最后我得到了我的代码。感谢大家。我想会帮助很多人。

var jsonResponse: NSMutableDictionary?
                                do{
                                    jsonResponse = try NSJSONSerialization.JSONObjectWithData(data!,
                                        options: NSJSONReadingOptions.AllowFragments) as? NSMutableDictionary;

                                    let info : NSArray =  jsonResponse!.valueForKey("latest_receipt_info") as! NSArray
                                    let transaction_id: String? = info[0].valueForKey("transaction_id") as? String
                                    let purchase_date: String? = info[0].valueForKey("purchase_date") as? String
                                    let product_id: String? = info[0].valueForKey("product_id") as? String
                                    let web_order_line_item_id: String? = info[0].valueForKey("web_order_line_item_id") as? String

                                    print("\(transaction_id!) \(purchase_date!) \(product_id!) \(web_order_line_item_id!) ")

谢谢!