How to parse JSON in objective-C iOS

2019-09-21 05:35发布

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

3条回答
成全新的幸福
2楼-- · 2019-09-21 06:04

Use objectForKey instead of valueForKey

NSDictionary *responseObject;
NSArray *arrAmount;
arrAmount = [responseObject objectForKey:@"row"];
NSLog(@"%@",arrAmount);
NSString *strAmount =[NSString stringWithFormat:@"%@", [[arrAmount objectAtIndex:0] objectForKey:@"amount"]];
查看更多
乱世女痞
3楼-- · 2019-09-21 06:07

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;
}
查看更多
萌系小妹纸
4楼-- · 2019-09-21 06:20

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);
查看更多
登录 后发表回答