除了Locale.getISOCountries()
之外,因为我已经getting some strange errors with that了。获取2个字母的国家/地区代码以及完整的国家/地区名称的最佳方法是什么?
答案 0 :(得分:42)
请参阅代码段:
String[] countryCodes = Locale.getISOCountries();
for (String countryCode : countryCodes) {
Locale obj = new Locale("", countryCode);
System.out.println("Country Code = " + obj.getCountry()
+ ", Country Name = " + obj.getDisplayCountry());
}
有关更多示例,请参阅此country list in Java。
答案 1 :(得分:4)
对于单独的项目,我从ISO site获取了国家/地区代码数据。
请注意以下事项:
答案 2 :(得分:1)
答案 3 :(得分:0)
您可以像这样从JSON波纹管中使用
Json解析..
String jsonString =JSON_DATA;
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode rootNode = mapper.readTree(jsonString);
for (JsonNode node : rootNode) {
String countrycode = node.path("code").asText();
String dialnumber = node.path("dial_code").asText();
String countryname = node.path("name").asText();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Json String here
public static String JSON_DATA="
[
{
"name": "Afghanistan",
"dial_code": "+93",
"code": "AF"
},
{
"name": "Aland Islands",
"dial_code": "+358",
"code": "AX"
},
{
"name": "Albania",
"dial_code": "+355",
"code": "AL"
},
{
"name": "Algeria",
"dial_code": "+213",
"code": "DZ"
},
{
"name": "AmericanSamoa",
"dial_code": "+1684",
"code": "AS"
}]";