2013-04-17 46 views
-2

這是我的字典結果。如何檢索所有國家的名稱與NSDictionary計數到NSArray?

"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。

+1

你可以發佈你的整個反應詞典嗎? – Madhu

+0

字典反應是世界上所有的國家的名字.. – user2243325

+0

所以這本字典包含countryNames和細節數組? – Madhu

回答

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"]); 
} 

希望,它會幫助你的。

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

祝您好運!

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

好運。