1

我的json响应是这样的。

{
    "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,然后在数组字段中比较这个值,然后在标题标签中显示是阿尔巴尼亚。

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

请帮我。

提前致谢。

4

2 回答 2

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 将包含选定的 id 国家名称;

于 2013-04-25T05:55:56.437 回答
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"];
于 2013-04-25T05:53:57.517 回答