2013-04-25 86 views
1

我的json响应如下。如何将选定的ID与数组中的国家ID进行比较并显示其名称不是ID

{ 
    "continent_code" = EU; 
    "country_code" = al; 
    "country_id" = 2; 
    "country_name" = Albania; 
}, 
{ 
    "continent_code" = AF; 
    "country_code" = dz; 
    "country_id" = 3; 
    "country_name" = Algeria; 
}, 

我的代码是这样的。

country_selected=2; 
NSMutableArray *wholeJsonArray = [LoginResult objectForKey:@"Response"]; 
    for(NSDictionary *countname in wholeJsonArray) 
    { 

     /* 
     NSString *cName = [NSString stringWithFormat:@"%@",[countname  
     objectForKey:@"country_name"]]; 
     [countryArray addObject:cName]; 
     */ 


     NSString *countryName = [countname objectForKey:@"country_name"]; 
     if(countryName) 
      [countryArray addObject:countryName]; 


     NSString *stateName=[countname objectForKey:@"state_name"]; 
     if(stateName) 
      [stateArray addObject:stateName]; 
    } 

    for(NSDictionary *cid in wholeJsonArray) 
    { 

     NSNumber *number = [cid objectForKey:@"country_id"]; 
     if(number) 
      [idcountry addObject:number]; 

     NSNumber *statenumber=[cid objectForKey:@"state_id"]; 
     if(statenumber) 
      [idstate addObject:statenumber]; 

    } 
} 

我的问题是,如果用户选择的国家ID 2然后比较在阵列字段这个值,然后在标题标签显示是阿尔巴尼亚。

那么我该怎么做这个输出?

请帮帮我。

在此先感谢。

回答

0

创建找国家从选择countryID

- (NSDictionary *)countryWithId:(NSUInteger)countryID 
{ 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"country_id == %d",countryID]; 
    return [[self.wholeJSONArray filteredArrayUsingPredicate:predicate]lastObject]; 

} 

您可以获取国家像

NSDictionary *country = [self countryWithId:2]; 
NSString *countryName = country[@"country_name"]; 

编辑的方法:或

country_selected=2; 
NSMutableArray *wholeJsonArray = [LoginResult objectForKey:@"Response"]; 

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"country_id == %d",countryID]; 
NSDictionary *country = [[wholeJsonArray filteredArrayUsingPredicate:predicate]lastObject]; 
NSString *countryName = country[@"country_name"]; 
1
country_selected=2; 
NSString *countryName; 
for(NSDictionary *countname in wholeJsonArray) 
{ 
    NSNumber *country_id = [countname objectForKey:@"country_id"]; 
    if(country_selected == country_id.integerValue){ 
     countryName = [countname objectForKey:@"country_name"]; 
    }   
} 

其中countryName将包含选定的国家/地区名称;

+0

非常感谢你 – 2013-04-25 06:15:51

相关问题