如何在 objective-C iOS 中解析 JSON

How to parse JSON in objective-C iOS

我想使用 AFNetworking 库在 Objective-C 中解析 JSON 在 iOS 中

{
  "success": 1,
  "row": [
    {
      "amount": 2800
    }
   ]
}

代码 -

 arrAmount = [responseObject valueForKey:@"row"];
 NSLog(@"%@",arrAmount);

 NSString *strAmount =[NSString stringWithFormat:@"%@", [arrAmount valueForKey:@"amount"]];

 self.lblAmount.text =[NSString stringWithFormat:@"%@",strAmount];

row的响应包含数组而不是字典,因此访问量来自数组的第一个对象。

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

使用 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);

使用objectForKey代替valueForKey

NSDictionary *responseObject;
NSArray *arrAmount;
arrAmount = [responseObject objectForKey:@"row"];
NSLog(@"%@",arrAmount);
NSString *strAmount =[NSString stringWithFormat:@"%@", [[arrAmount objectAtIndex:0] objectForKey:@"amount"]];