解析IAP收据的JSON

时间:2016-02-19 20:59:55

标签: ios json swift

我最近为我的应用程序实现了收据验证功能,但是我很难解析收据。我可以通过这样做来获得它的状态

verifyStatus = json!["status"]

然后分别来自

的'收据'和'in_app'值
let verifyItem2 = json!.objectForKey("receipt")
let verifyItem3 = verifyItem2!.objectForKey("in_app")

然而,当我为'product_id'做同样的事情时。我一直在收到错误。可能是因为'in_app'键在它之前有一组额外的括号。一些建议将不胜感激。

示例收据:

environment = Sandbox;
receipt =     {
    "adam_id" = 0;
    "app_item_id" = 0;
    "application_version" = 1;
    "bundle_id" = "xxxxxxxx";
    "download_id" = 0;
    "in_app" =         (
                    {
            "is_trial_period" = false;
            "original_purchase_date" = "2016-02-16 05:47:31 Etc/GMT";
            "original_purchase_date_ms" = 1455601651000;
            "original_purchase_date_pst" = "2016-02-15 21:47:31 America/Los_Angeles";
            "original_transaction_id" = 1000000194125405;
            "product_id" = "xxxxxxxx";
            "purchase_date" = "2016-02-16 05:47:31 Etc/GMT";
            "purchase_date_ms" = 1455601651000;
            "purchase_date_pst" = "2016-02-15 21:47:31 America/Los_Angeles";
            quantity = 1;
            "transaction_id" = 1000000194122312;
        }
    );
    "original_application_version" = "1.0";
    "original_purchase_date" = "2013-08-01 07:00:00 Etc/GMT";
    "original_purchase_date_ms" = 1375340400000;
    "original_purchase_date_pst" = "2013-08-01 00:00:00 America/Los_Angeles";
    "receipt_creation_date" = "2016-02-19 20:49:30 Etc/GMT";
    "receipt_creation_date_ms" = 1455914970000;
    "receipt_creation_date_pst" = "2016-02-19 12:49:30 America/Los_Angeles";
    "receipt_type" = ProductionSandbox;
    "request_date" = "2016-02-19 20:49:34 Etc/GMT";
    "request_date_ms" = 1455914974827;
    "request_date_pst" = "2016-02-19 12:49:34 America/Los_Angeles";
    "version_external_identifier" = 0;
};
status = 0;

})

1 个答案:

答案 0 :(得分:1)

in_app字段是一个数组,所以你必须遍历它

for inApp in verifyItem3 {
    // do something with that dictionary, for example 
    print(inApp["product_id"])
}

请注意我在这里写了这段代码,所以可能会有错误:)