Darshan Sonigara Darshan Sonigara - 2 months ago 8
iOS Question

How to parse JSON in objective-C iOS

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];

Answer

Response of rowcontain 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;
}
Comments