我的 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,然后在数组字段中比较这个值,然后在标题标签中显示是阿尔巴尼亚。
那么我该如何做这个输出呢?
请帮帮我。
提前致谢。
最佳答案
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 国家名称;
关于iphone - 如何将所选 ID 与数组中的国家/地区 ID 进行比较并显示其名称而不是 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16207266/