在API响应中,我将从服务器获得ISOα-2国家代码,但我需要将ISOα-2国家代码转换为国家名称。我正在使用Java 8。
发布于 2021-10-11 16:24:47
使用下面的代码,我们可以从Java8语言环境中获得所有国家的ISO3代码和ISO2代码,以及国家名称。
public static void main(String[] args) throws Exception {
String[] isoCountries = Locale.getISOCountries();
for (String country : isoCountries) {
Locale locale = new Locale("en", country);
String iso = locale.getISO3Country();
String code = locale.getCountry();
String name = locale.getDisplayCountry();
System.out.println(iso + " " + code + " " + name);
}
}您还可以创建一个查找表映射,以便在ISO代码之间进行转换。因为我需要在iso3和iso2之间进行转换,所以按照。
String[] isoCountries = Locale.getISOCountries();
Map<String, String> countriesMapping = new HashMap<String, String>();
for (String country : isoCountries) {
Locale locale = new Locale("en", country);
String iso = locale.getISO3Country();
String code = locale.getCountry();
String name = locale.getDisplayCountry();
countriesMapping.put(iso, code);
}发布于 2021-09-28 15:05:47
试着看看这个问题的答案:Is there an open source java enum of ISO 3166-1 country codes
您应该能够添加一个库到您的应用程序,让您获得一个国家的名称通过代码。
https://stackoverflow.com/questions/69362699
复制相似问题