问题
I want to parse JSON in Objective-C in iOS using AFNetworking library
{
"success": 1,
"row": [
{
"amount": 2800
}
]
}
CODE -
arrAmount = [responseObject valueForKey:@"row"];
NSLog(@"%@",arrAmount);
NSString *strAmount =[NSString stringWithFormat:@"%@", [arrAmount valueForKey:@"amount"]];
self.lblAmount.text =[NSString stringWithFormat:@"%@",strAmount];
回答1:
Response of row
contain array not Dictionary, so access amount from first object of Array.
NSarray *arrAmount = [responseObject objectForKey:@"row"];
if arrAmount.count > 0 {
NSDictionary *dic = [arrAmount objectAtIndex:0];
NSString *strAmount = [NSString stringWithFormat:@"%d", [dic objectForKey:@"amount"]];
self.lblAmount.text = strAmount;
}
回答2:
use NSDictionary:
NSDictionary *dict = [responseObject valueForKey:@"row"];
NSLog(@"value = %@",[dict valueForKey:@"amount"]); // fetch amount value
array = [dict valueForKey:@"amount"]; // // fetch all amount value in array
NSLog(@"array = %@", array.description);
回答3:
Use objectForKey
instead of valueForKey
NSDictionary *responseObject;
NSArray *arrAmount;
arrAmount = [responseObject objectForKey:@"row"];
NSLog(@"%@",arrAmount);
NSString *strAmount =[NSString stringWithFormat:@"%@", [[arrAmount objectAtIndex:0] objectForKey:@"amount"]];
来源:https://stackoverflow.com/questions/39487854/how-to-parse-json-in-objective-c-ios