如何通过从NSDictionary计数到NSArray来检索所有国家/地区名称?

时间:2013-04-17 06:39:01

标签: iphone ios nsmutablearray nsdictionary

这是我的字典结果。

"continent_code" = EU;
"country_code" = gb;
"country_id" = 169;
"country_name" = "United Kingdom";


NSString *responseString = [[NSString alloc] initWithData:responseData      
encoding:NSUTF8StringEncoding];
NSDictionary *LoginResult = (NSDictionary*)[responseString JSONValue];

我想从字典中仅检索NSArray的国家/地区名称。

3 个答案:

答案 0 :(得分:0)

试试这个::

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:webData options:NSJSONReadingMutableContainers error:&error];

NSLog(@" Value :: %@", [[jsonArray JSONRepresentation] JSONValue]);

for (NSDictionary *item in jsonArray) {
   NSLog(@" Item :::::> %@", [item objectForKey:@"country_name"]);
}

希望,它会帮助你。

答案 1 :(得分:0)

 NSMutableArray *wholeJsonArray = [LoginResult objectForKey:@"RootName"];
 NSMutableArray *loginArray = [[NSMutableArray alloc]init];

 for(int i = 0 ; i<[loginArray count];i++)
    {
        NSString *countryName=[[loginArray objectAtIndex:i]objectForKey:@"country_name"];
       [loginArray addObject:countryName];
    }
祝你好运!!

答案 2 :(得分:0)

填写 countryArray 的正确方法:

NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:yourResponseData options:kNilOptions error:&error];
NSArray* getMAS_CR = [(NSDictionary*)json objectForKey:@"yourKeyRootResult"];
NSMutableArray *countryArray = [[NSMutableArray alloc]init];
for (NSDictionary *rr in getMAS_CR)
{
    NSString *cName = [NSString stringWithFormat:@"%@",[rr objectForKey:@"country_name"]];
    [countryArray addObject:cName];
}
NSLog(@"countryArray :: %@",countryArray);

好运。