objective c 在 UIAlertView 中显示 NSDictionary 值

objective c show NSDictionary value in UIAlertView

我有一个 NSDictionary,下面是我的 NSDictionary。

 NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];

而 NSMutableDictionary 输出是:

{
    "access-numbers" =     (
                {
            "access-number" = 8822114477;
            "toll-free" = 0;
        },
                {
            "access-number" = 4353;
            "toll-free" = 1;
        }
    );
    "area-prefix" = "-1";
    "country-prefix" = 880;
    status = 200;
}

我想在 UIAlertView "access-number" = 8822114477;"access-number" = 4353;

中显示

现在我这样覆盖它

  NSString *telAcceesNumber=[[jsonDic valueForKeyPath:@"access-numbers"][0] objectForKey:@"access-number"];
    NSString *tolfree=[[jsonDic valueForKeyPath:@"access-numbers"][1] objectForKey:@"access-number"];

并在 UIAlertView 上显示

   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Accesss Number"
                                                        message:Nil
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:telAcceesNumber,tolfree,nil];
        [alert show];

但我想使用 for-loop 获取我的两个访问号码并显示在 UIAlertView 上。因为有时候access-number可能只有一次或者三次以上。

你能帮忙解决我的问题吗

提前致谢

您需要以这样的方式构造一个 NSString,它应该同时获取键和值以及下一行 \n

NSString *displayString = [NSString stringWithFormat:@"%@ - %@ \n",key, [jsonArray valueForKey:key]];

并向其添加更多键值。完成后,您可以轻松地在 UIAlertController.

中显示

您可以使用以下代码从字典中使用键 "access-numbers" 获取您想要的所有值:

NSMutableArray *allAccessNumbers = [[NSMutableArray alloc] init];
NSArray *accessNumbers = jsonDict[@"access-numbers"];
for (NSDictionary *current in accessNumbers)
{
    NSString *accessNumber = current[@"access-number"];
    [allAccessNumbers addObject:accessNumber];
}

稍后,您可以使用 allAccessNumbers 的值构建 NSString 以在警报视图中显示。如果您将来不需要单独访问每个访问号码,您甚至可以在 for 循环中构建 NSString。我希望这能回答你的问题。