This is my json:
"latest_receipt_info" = (
{
"expires_date" = "2022-02-15 07:33:27 Etc/GMT";
"is_in_intro_offer_period" = false;
"is_trial_period" = false;
"product_id" = "com.apollo66.InAppPurchasePT.AutoRenewableGroup.ARenewable6";
"transaction_id" = 1000000968989231;
"web_order_line_item_id" = 1000000072680194;
},
{
"expires_date" = "2022-02-15 07:33:27 Etc/GMT";
"is_in_intro_offer_period" = false;
"is_trial_period" = false;
"product_id" = "com.apollo66.InAppPurchasePT.AutoRenewableGroup.ARenewable6";
"transaction_id" = 1000000968989231;
"web_order_line_item_id" = 1000000072680194;
"cancellation_date" = "2019-12-05 19:14:48 Etc/GMT",
}
);
As you can see there are 1 extra attributes in the 1st dictionary. Now I want to make an object for this kind of dictionary where sometimes "cancellation_date": "***" attribute could appear and sometimes not. How can I do it?
This is my object:
struct LatestReceiptInfo {
var expiresDate: Date? = nil
let isInIntroOfferPeriod: Bool
let isTrialPeriod: Bool
let productId : String
var cancellationDate: Date? = nil
}
And this is how I'm trying to bind the values:
for receiptInf in receiptInfo {
let recInf = receiptInf as! NSDictionary
guard let expiresDate = recInf["expires_date"] as? Date,
let isInIntroOfferPeriod = recInf["is_in_intro_offer_period"] as? Bool,
let isTrialPeriod = recInf["is_trial_period"] as? Bool,
let productId = recInf["product_id"] as? String else {
print("Something is not well")
continue
}
let latestReceiptInfo = LatestReceiptInfo(expiresDate: expiresDate, isInIntroOfferPeriod: isInIntroOfferPeriod, isTrialPeriod: isTrialPeriod, productId: productId, cancellationDate: nil)
latestReceiptInfoArray.append(latestReceiptInfo)
}
But it's not working.
Here is an example with notes
1- Any json should be surrounded with
{}or[]and yours isn't2- You have an invalid json
3- You make a bool a string with "false" while it should be false
4- For supporting properties without _ you need to set
keyDecodingStrategyto.convertFromSnakeCaseand5- For supporting string dates you need to assign a formatter to
dateDecodingStrategy