2013-07-26 130 views
1

有没有办法获得特定国家的大陆?每个国家的排序国家

我使用乱舞的API,它返回由国家分开的JSON,我需要在Contintents的数据,所以我想用自己的国家内的每个contintent阵列,例如:

NSArray * europe = [[NSArray alloc] initWithObjects:@"ES", @"IT", @"FR", ... , nil]; 

谢谢提前!

+0

看在上帝的份上,使用下标:'NSArray的*欧洲= @ @ “ES”,@ “IT”,@ “EN”,...]' – Cyrille

+0

如果'JSON'包含大陆或只有国家的变量吗? – user2545330

+0

您是否正在寻找一个web服务,可以获得大陆所有国家的列表? –

回答

0

您可以使用此:

private static HashMap<String,String> countryMap=new HashMap<String,String>(); 
private final static String EUROPE="E"; 
private final static String NORTH_AMERICA="N"; 
private final static String SOUTH_AMERICA="S"; 
private final static String AFRICA="AF"; 
private final static String ASIA="AS"; 
private final static String OCEANIA="O"; 
private final static String ANTARTIDA="ANT"; 

static 
    { 
    countryMap.put("AF", "AS"); 
    countryMap.put("AL", "E"); 
    countryMap.put("DE", "E"); 
    countryMap.put("DZ", "E"); 
    countryMap.put("AD", "E"); 
    countryMap.put("AQ", "ANT"); 
    countryMap.put("AG", "S"); 
    countryMap.put("SA", "AS"); 
    countryMap.put("AR", "S"); 
    countryMap.put("AM", "AS"); 
    countryMap.put("AW", "S"); 
    countryMap.put("AU", "O"); 
    countryMap.put("AZ", "AS"); 
    countryMap.put("AT", "E"); 
    countryMap.put("BS", "S"); 
    countryMap.put("BH", "AS"); 
    countryMap.put("BD", "AS"); 
    countryMap.put("BB", "S"); 
    countryMap.put("BZ", "S"); 
    countryMap.put("BJ", "AF"); 
    countryMap.put("BM", "S"); 
    countryMap.put("BE", "E"); 
    countryMap.put("BT", "AS"); 
    countryMap.put("BE", "E"); 
    countryMap.put("BO", "S"); 
    countryMap.put("BW", "AF"); 
    countryMap.put("BA", "E"); 
    countryMap.put("BR", "S"); 
    countryMap.put("BN", "AS"); 
    countryMap.put("BG", "E"); 
    countryMap.put("BF", "AF"); 
    countryMap.put("BI", "AF"); 
    countryMap.put("KH", "AS"); 
    countryMap.put("CM", "AF"); 
    countryMap.put("CA", "N"); 
    countryMap.put("CV", "AF"); 
    countryMap.put("BQ", "S"); 
    countryMap.put("AF", "AS"); 
    countryMap.put("CO", "S"); 
    countryMap.put("KM", "AF"); 
    countryMap.put("CG", "AF"); 
    countryMap.put("CD", "AF"); 
    countryMap.put("KP", "AS"); 
    countryMap.put("KR", "AS"); 
    countryMap.put("CR", "S"); 
    countryMap.put("CI", "AF"); 
    countryMap.put("HR", "E"); 
    countryMap.put("CU", "S"); 
    countryMap.put("CW", "S"); 
    countryMap.put("DK", "E"); 
    countryMap.put("DJ", "AF"); 
    countryMap.put("DM", "S"); 
    countryMap.put("EG", "AF"); 
    countryMap.put("SV", "S"); 
    countryMap.put("EC", "S"); 
    countryMap.put("ER", "AF"); 
    countryMap.put("SK", "E"); 
    countryMap.put("SI", "E"); 
    countryMap.put("ES", "E"); 
    countryMap.put("US", "N"); 
    countryMap.put("EE", "E"); 
    countryMap.put("ET", "AF"); 
    countryMap.put("FJ", "O"); 
    countryMap.put("PH", "AS"); 
    countryMap.put("FI", "E"); 
    countryMap.put("FR", "E"); 
    countryMap.put("GA", "AF"); 
    countryMap.put("GM", "AF"); 
    countryMap.put("GE", "E"); 
    countryMap.put("GH", "AF"); 
    countryMap.put("GI", "E"); 
    countryMap.put("GD", "S"); 
    countryMap.put("GL", "N"); 
    countryMap.put("GP", "S"); 
    countryMap.put("GF", "S"); 
    countryMap.put("GU", "AF"); 
    countryMap.put("GT", "S"); 
    countryMap.put("GG", "E"); 
    countryMap.put("GN", "AF"); 
    countryMap.put("GW", "AF"); 
    countryMap.put("GQ", "AF"); 
    countryMap.put("GY", "S"); 
    countryMap.put("HT", "S"); 
    countryMap.put("HN", "S"); 
    countryMap.put("HN", "E"); 
    countryMap.put("IE", "AS"); 
    countryMap.put("BV", "ANT"); 
    countryMap.put("CX", "O"); 
    countryMap.put("HM", "ANT"); 
    countryMap.put("AF", "O"); 
    countryMap.put("IM", "E"); 
    countryMap.put("RE", "AF"); 
    countryMap.put("KY", "S"); 
    countryMap.put("CC", "O"); 
    countryMap.put("CK", "O"); 
    countryMap.put("FO", "E"); 
    countryMap.put("GS", "ANT"); 
    countryMap.put("FK", "S"); 
    countryMap.put("MP", "O"); 
    countryMap.put("MH", "O"); 
    countryMap.put("UM", "O"); 
    countryMap.put("PN", "S"); 
    countryMap.put("SB", "O"); 
    countryMap.put("TC", "S"); 
    countryMap.put("VG", "S"); 
    countryMap.put("VI", "S"); 
    countryMap.put("ID", "AS"); 
    countryMap.put("IR", "AS"); 
    countryMap.put("IQ", "AS"); 
    countryMap.put("IE", "E"); 
    countryMap.put("IS", "E"); 
    countryMap.put("IL", "AS"); 
    countryMap.put("IT", "E"); 
    countryMap.put("IN", "AS"); 
    countryMap.put("JM", "S"); 
    countryMap.put("JP", "AS"); 
    countryMap.put("JE", "E"); 
    countryMap.put("JO", "AS"); 
    countryMap.put("KZ", "AS"); 
    countryMap.put("KE", "AF"); 
    countryMap.put("KG", "AS"); 
    countryMap.put("KI", "O"); 
    countryMap.put("KW", "AS"); 
    countryMap.put("LA", "AS"); 
    countryMap.put("LV", "E"); 
    countryMap.put("LR", "AF"); 
    countryMap.put("LI", "E"); 
    countryMap.put("LT", "E"); 
    countryMap.put("LB", "AS"); 
    countryMap.put("LY", "AF"); 
    countryMap.put("LU", "E"); 
    countryMap.put("MK", "E"); 
    countryMap.put("MG", "AF"); 
    countryMap.put("MW", "AF"); 
    countryMap.put("MV", "AS"); 
    countryMap.put("ML", "AF"); 
    countryMap.put("MT", "E"); 
    countryMap.put("MA", "AF"); 
    countryMap.put("MQ", "S"); 
    countryMap.put("MU", "AF"); 
    countryMap.put("MR", "AF"); 
    countryMap.put("YT", "AF"); 
    countryMap.put("MX", "N"); 
    countryMap.put("FM", "O"); 
    countryMap.put("MZ", "AF"); 
    countryMap.put("MD", "E"); 
    countryMap.put("MN", "AS"); 
    countryMap.put("ME", "E"); 
    countryMap.put("MS", "S"); 
    countryMap.put("MC", "E"); 
    countryMap.put("MM", "AS"); 
    countryMap.put("NA", "AF"); 
    countryMap.put("NR", "O"); 
    countryMap.put("NP", "AS"); 
    countryMap.put("NI", "S"); 
    countryMap.put("NG", "AF"); 
    countryMap.put("NU", "O"); 
    countryMap.put("NE", "AF"); 
    countryMap.put("NO","E"); 
    countryMap.put("NC", "O"); 
    countryMap.put("NZ", "O"); 
    countryMap.put("OM", "AS"); 
    countryMap.put("NL", "E"); 
    countryMap.put("PK", "AS"); 
    countryMap.put("PW", "O"); 
    countryMap.put("PS", "AS"); 
    countryMap.put("PA", "S"); 
    countryMap.put("PG", "O"); 
    countryMap.put("PY", "S"); 
    countryMap.put("PE", "S"); 
    countryMap.put("PF", "O"); 
    countryMap.put("PL", "E"); 
    countryMap.put("PT", "E"); 
    countryMap.put("PR", "S"); 
    countryMap.put("QA", "AS"); 
    countryMap.put("HK", "AS"); 
    countryMap.put("MO", "AS"); 
    countryMap.put("GB", "E"); 
    countryMap.put("CF", "E"); 
    countryMap.put("DO", "S"); 
    countryMap.put("CZ", "E"); 
    countryMap.put("ZA", "AF"); 
    countryMap.put("RO", "E"); 
    countryMap.put("RU", "E"); 
    countryMap.put("RW", "AF"); 
    countryMap.put("UM", "S"); 
    countryMap.put("KN", "S"); 
    countryMap.put("SH", "AF"); 
    countryMap.put("LC", "S"); 
    countryMap.put("MF", "S"); 
    countryMap.put("VC", "S"); 
    countryMap.put("PM", "N"); 
    countryMap.put("WS", "O"); 
    countryMap.put("AS", "O"); 
    countryMap.put("SM", "E"); 
    countryMap.put("EH", "AF"); 
    countryMap.put("ST", "AF"); 
    countryMap.put("SN", "AF"); 
    countryMap.put("SC", "AF"); 
    countryMap.put("RS", "E"); 
    countryMap.put("SL", "AF"); 
    countryMap.put("SG", "AS");   
    countryMap.put("SR", "S"); 
    countryMap.put("SJ", "E"); 
    countryMap.put("SZ", "AF"); 
    countryMap.put("TJ", "AS"); 
    countryMap.put("TH", "AS"); 
    countryMap.put("TW", "AS"); 
    countryMap.put("TZ", "AF"); 
    countryMap.put("IO", "O"); 
    countryMap.put("TF", "O"); 
    countryMap.put("TL", "AS"); 
    countryMap.put("TG", "AF"); 
    countryMap.put("TK", "O"); 
    countryMap.put("TO", "O"); 
    countryMap.put("TT", "S"); 
    countryMap.put("TN", "AF"); 
    countryMap.put("TM", "AS"); 
    countryMap.put("TR", "AS"); 
    countryMap.put("TV", "O"); 
    countryMap.put("TD", "AF"); 
    countryMap.put("UA", "E"); 
    countryMap.put("UG", "AF"); 
    countryMap.put("AE", "AS"); 
    countryMap.put("UY", "S"); 
    countryMap.put("UZ", "AS"); 
    countryMap.put("VZ", "O"); 
    countryMap.put("VA", "E"); 
    countryMap.put("VE", "S"); 
    countryMap.put("VN", "AS"); 
    countryMap.put("WF", "O"); 
    countryMap.put("CL", "S"); 
    countryMap.put("CN", "AS"); 
    countryMap.put("CY", "E"); 
    countryMap.put("ZM", "AF"); 
    countryMap.put("ZW", "AF"); 
    } 
+0

这不是客观的-C。 – Cyrille

2

声明一个反向查找词典:

NSDictionary *continentForCountries = @{ 
    @"FR": @"Europe", 
    @"IT": @"Europe", 
    @"US": @"North America", 
    @"CA": @"North America", 
    ... 
} 

然后您可以将国家分为使用enumerateObjectsUsingBlock,里面你积累国家陷入阵列,它的键是大陆名称词典洲:

NSArray *countries = @[...]; 
NSArray *continentForCountries = @[...]; 

NSMutableDictionary *countriesByContinents = [NSMutableDictionary new]; 
[countries enumerateObjectsUsingBlock:^(NSString *country, NSUInteger idx, BOOL *stop) { 
    NSString *continent = continentForCountries[country]; 
    if (countriesByContinents[continent] == nil) 
     countriesByContinents[continent] = [NSMutableArray new]; 
    [countriesByContinents[continent] addObject:country]; 
}]; 
+0

请注意,我不处理您在'continentForCountries'中忘记条目的情况,这只是一个练习。 – Cyrille

+0

这是一个很好的方法,但我需要一个已经完成的nsdictionary或nsarrays。由于我没有找到任何东西,我会自己做。谢谢! PD:我觉得对旧声明更舒适;)老方法...... –

+0

然后,也许你会在这里找到你想要的:http://webmasters.stackexchange.com/questions/26214/where-can-i -get-iso-lists-for-continent-country-and-currency-to-use-my-web – Cyrille

1

她e是我创建了的NSDictionary:

countryPerContinent = @{@"AF":@"AS", 
          @"AL":@"E", 
          @"DE":@"E", 
          @"DZ":@"E", 
          @"AD":@"E", 
          @"AQ":@"ANT", 
          @"AG":@"S", 
          @"SA":@"AS", 
          @"AR":@"S", 
          @"AM":@"AS", 
          @"AW":@"S", 
          @"AU":@"O", 
          @"AZ":@"AS", 
          @"AT":@"E", 
          @"BS":@"S", 
          @"BH":@"AS", 
          @"BD":@"AS", 
          @"BB":@"S", 
          @"BZ":@"S", 
          @"BJ":@"AF", 
          @"BM":@"S", 
          @"BE":@"E", 
          @"BT":@"AS", 
          @"BO":@"S", 
          @"BW":@"AF", 
          @"BA":@"E", 
          @"BR":@"S", 
          @"BN":@"AS", 
          @"BG":@"E", 
          @"BF":@"AF", 
          @"BI":@"AF", 
          @"KH":@"AS", 
          @"CM":@"AF", 
          @"CA":@"N", 
          @"CV":@"AF", 
          @"BQ":@"S", 
          @"AF":@"AS", 
          @"CO":@"S", 
          @"KM":@"AF", 
          @"CG":@"AF", 
          @"CD":@"AF", 
          @"KP":@"AS", 
          @"KR":@"AS", 
          @"CR":@"S", 
          @"CI":@"AF", 
          @"HR":@"E", 
          @"CU":@"S", 
          @"CW":@"S", 
          @"DK":@"E", 
          @"DJ":@"AF", 
          @"DM":@"S", 
          @"EG":@"AF", 
          @"SV":@"S", 
          @"EC":@"S", 
          @"ER":@"AF", 
          @"SK":@"E", 
          @"SI":@"E", 
          @"ES":@"AF", 
          @"US":@"N", 
          @"EE":@"E", 
          @"ET":@"AF", 
          @"FJ":@"O", 
          @"PH":@"AS", 
          @"FI":@"E", 
          @"FR":@"E", 
          @"GA":@"AF", 
          @"GM":@"AF", 
          @"GE":@"E", 
          @"GH":@"AF", 
          @"GI":@"E", 
          @"GD":@"S", 
          @"GL":@"N", 
          @"GP":@"S", 
          @"GF":@"S", 
          @"GU":@"AF", 
          @"GT":@"S", 
          @"GG":@"E", 
          @"GN":@"AF", 
          @"GW":@"AF", 
          @"GQ":@"AF", 
          @"GY":@"S", 
          @"HT":@"S", 
          @"HN":@"S", 
          @"HU":@"E", 
          @"IE":@"AS", 
          @"BV":@"ANT", 
          @"CX":@"O", 
          @"HM":@"ANT", 
          @"AF":@"O", 
          @"IM":@"E", 
          @"RE":@"AF", 
          @"KY":@"S", 
          @"CC":@"O", 
          @"CK":@"O", 
          @"FO":@"E", 
          @"GS":@"ANT", 
          @"FK":@"S", 
          @"MP":@"O", 
          @"MH":@"O", 
          @"UM":@"O", 
          @"PN":@"S", 
          @"SB":@"O", 
          @"TC":@"S", 
          @"VG":@"S", 
          @"VI":@"S", 
          @"ID":@"AS", 
          @"IR":@"AS", 
          @"IQ":@"AS", 
          @"IE":@"E", 
          @"IS":@"E", 
          @"IL":@"AS", 
          @"IT":@"E", 
          @"IN":@"AS", 
          @"JM":@"S", 
          @"JP":@"AS", 
          @"JE":@"E", 
          @"JO":@"AS", 
          @"KZ":@"AS", 
          @"KE":@"AF", 
          @"KG":@"AS", 
          @"KI":@"O", 
          @"KW":@"AS", 
          @"LA":@"AS", 
          @"LV":@"E", 
          @"LR":@"AF", 
          @"LI":@"E", 
          @"LT":@"E", 
          @"LB":@"AS", 
          @"LY":@"AF", 
          @"LU":@"E", 
          @"MK":@"E", 
          @"MG":@"AF", 
          @"MW":@"AF", 
          @"MV":@"AS", 
          @"ML":@"AF", 
          @"MT":@"E", 
          @"MA":@"AF", 
          @"MQ":@"S", 
          @"MU":@"AF", 
          @"MR":@"AF", 
          @"YT":@"AF", 
          @"MX":@"N", 
          @"FM":@"O", 
          @"MZ":@"AF", 
          @"MD":@"E", 
          @"MN":@"AS", 
          @"ME":@"E", 
          @"MS":@"S", 
          @"MC":@"E", 
          @"MM":@"AS", 
          @"NA":@"AF", 
          @"NR":@"O", 
          @"NP":@"AS", 
          @"NI":@"S", 
          @"NG":@"AF", 
          @"NU":@"O", 
          @"NE":@"AF", 
          @"NO":@"E", 
          @"NC":@"O", 
          @"NZ":@"O", 
          @"OM":@"AS", 
          @"NL":@"E", 
          @"PK":@"AS", 
          @"PW":@"O", 
          @"PS":@"AS", 
          @"PA":@"S", 
          @"PG":@"O", 
          @"PY":@"S", 
          @"PE":@"S", 
          @"PF":@"O", 
          @"PL":@"E", 
          @"PT":@"E", 
          @"PR":@"S", 
          @"QA":@"AS", 
          @"HK":@"AS", 
          @"MO":@"AS", 
          @"GB":@"E", 
          @"CF":@"E", 
          @"DO":@"S", 
          @"CZ":@"E", 
          @"ZA":@"AF", 
          @"RO":@"E", 
          @"RU":@"E", 
          @"RW":@"AF", 
          @"UM":@"S", 
          @"KN":@"S", 
          @"SH":@"AF", 
          @"LC":@"S", 
          @"MF":@"S", 
          @"VC":@"S", 
          @"PM":@"N", 
          @"WS":@"O", 
          @"AS":@"O", 
          @"SM":@"E", 
          @"EH":@"AF", 
          @"ST":@"AF", 
          @"SN":@"AF", 
          @"SC":@"AF", 
          @"RS":@"E", 
          @"SL":@"AF", 
          @"SG":@"AS", 
          @"SR":@"S", 
          @"SJ":@"E", 
          @"SZ":@"AF", 
          @"TJ":@"AS", 
          @"TH":@"AS", 
          @"TW":@"AS", 
          @"TZ":@"AF", 
          @"IO":@"O", 
          @"TF":@"O", 
          @"TL":@"AS", 
          @"TG":@"AF", 
          @"TK":@"O", 
          @"TO":@"O", 
          @"TT":@"S", 
          @"TN":@"AF", 
          @"TM":@"AS", 
          @"TR":@"AS", 
          @"TV":@"O", 
          @"TD":@"AF", 
          @"UA":@"E", 
          @"UG":@"AF", 
          @"AE":@"AS", 
          @"UY":@"S", 
          @"UZ":@"AS", 
          @"VZ":@"O", 
          @"VA":@"E", 
          @"VE":@"S", 
          @"VN":@"AS", 
          @"WF":@"O", 
          @"CL":@"S", 
          @"CN":@"AS", 
          @"CY":@"E", 
          @"ZM":@"AF", 
          @"ZW":@"AF"};