除了 Locale.getISOCountries()
之外,因为我已经遇到了一些奇怪的错误(相关问题)。还有什么是获取两位字母国家代码和全称的最佳方法?
Locale.getISOCountries()
外,有没有其他更好的方式来获取两个字母的国家代码和完整的国家名称呢?查看代码片段:
String[] countryCodes = Locale.getISOCountries();
for (String countryCode : countryCodes) {
Locale obj = new Locale("", countryCode);
System.out.println("Country Code = " + obj.getCountry()
+ ", Country Name = " + obj.getDisplayCountry());
}
查看此Java国家列表以获取更多示例。
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字符串
public static String JSON_DATA="
[
{
"name": "阿富汗",
"dial_code": "+93",
"code": "AF"
},
{
"name": "奥兰群岛",
"dial_code": "+358",
"code": "AX"
},
{
"name": "阿尔巴尼亚",
"dial_code": "+355",
"code": "AL"
},
{
"name": "阿尔及利亚",
"dial_code": "+213",
"code": "DZ"
},
{
"name": "美属萨摩亚",
"dial_code": "+1684",
"code": "AS"
}]";
或者您可以从链接https://gist.github.com/Goles/3196253下载完整的json文件。