这是NSDictionary的输出:
{
client = {
environment = mock;
"paypal_sdk_version" = "1.0.3";
platform = iOS;
"product_name" = "PayPal iOS SDK";
};
payment = {
amount = "39.95";
"currency_code" = USD;
"short_description" = "Awesome saws";
};
"proof_of_payment" = {
"adaptive_payment" = {
"app_id" = "APP-1245783590";
"pay_key" = "AP-70M62356425642W";
"payment_exec_status" = COMPLETED;
timestamp = "2012-03-03T15:53:55Z";
};
};
来自Paypal Adaptive Payments。我最终这样做了:
- (void)verifyCompletedPayment:(PayPalPayment *)completedPayment {
NSDictionary *pOPDictionary = [NSDictionary dictionaryWithDictionary:completedPayment.confirmation];
NSLog(@"pOPDictionary: %@",pOPDictionary);
NSDictionary *subDictionary = [pOPDictionary objectForKey:@"proof_of_payment"];
NSDictionary *sub2Dictionary = [subDictionary objectForKey:@"adaptive_payment"];
NSString *proofString = [sub2Dictionary objectForKey:@"payment_exec_status"];
if ([proofString isEqualToString:@"COMPLETED"]) {
NSLog(@"Payment Completed Successfully");
} else {
NSLog(@"Payment Error");
}
NSData *confirmation = [NSJSONSerialization dataWithJSONObject:completedPayment.confirmation options:0 error:nil];
}
是否有更有效的方法从字典词典中获取该密钥?我真的只需要那把钥匙。所以我不认为我应该循环或循环通过其他人。
答案 0 :(得分:0)
如何简化代码有两种不同的可能性。您可以使用“Modern Objective-C下标语法”(有关详细信息,请参阅http://clang.llvm.org/docs/ObjectiveCLiterals.html):
NSDictionary *pOPDictionary = completedPayment.confirmation;
NSString *proofString = pOPDictionary[@"proof_of_payment"][@"proof_of_payment"][@"payment_exec_status"];
或键值编码:
NSDictionary *pOPDictionary = completedPayment.confirmation;
NSString *proofString = [pOPDictionary valueForKeyPath:@"proof_of_payment.proof_of_payment.payment_exec_status"];
我不认为一方面有优势,这只是一个问题 品尝你选择哪一个。