获取 Java 国家/地区列表的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/712231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to get a list of countries in Java
提问by Click Upvote
Other than Locale.getISOCountries()
that is, because I'm already getting some strange errors with that. What else is the best way to get the 2-letter country codes as well as the full country name?
除此之外Locale.getISOCountries()
,因为我已经遇到了一些奇怪的错误。获取 2 个字母的国家/地区代码以及完整的国家/地区名称的最佳方法是什么?
采纳答案by user85509
For a separate project, I took the country code data from the ISO site.
对于一个单独的项目,我从ISO 站点获取了国家/地区代码数据。
Beware of the following:
请注意以下事项:
- The names are in all caps. You will probably want to tweak it so it's not.
- The names are not all in simple ASCII.
- The names are not entirely political neutral (it is probably impossible for any purported list of countries to be). E.g., "Taiwan, Province of China" is a name. A good starting point to learn about the issues is this blog post.
- 名称全部大写。你可能想要调整它,所以它不是。
- 这些名称并非都是简单的 ASCII。
- 这些名字并不完全是中立的(任何所谓的国家名单都可能是不可能的)。例如,“中国台湾省”是一个名称。了解这些问题的一个很好的起点是这篇博文。
回答by KitsuneYMG
- Create a Map out of this page http://www.theodora.com/country_digraphs.html
- Save it to a file (I suggest XMLEncoder/XMLDecoder class)
- Create a wrapping class that loads this Map from the file (I'd use a lazily initialized singleton) and allows access to the get(...) methods.
- Repeat (or use a bi-directional map) these steps for each column of the table on the afore mentioned webpage.
- Fancy-Time: Throw in some code to wrap the entries in a Reference object (SoftReference?) so that the Map won't throw MemoryErrors
- 从此页面创建地图http://www.theodora.com/country_digraphs.html
- 将其保存到文件中(我建议使用 XMLEncoder/XMLDecoder 类)
- 创建一个从文件加载这个 Map 的包装类(我会使用一个延迟初始化的单例)并允许访问 get(...) 方法。
- 对上述网页上表格的每一列重复(或使用双向映射)这些步骤。
- Fancy-Time:放入一些代码将条目包装在 Reference 对象(SoftReference?)中,以便 Map 不会抛出 MemoryErrors
回答by mkyong
See code snippets :
查看代码片段:
String[] countryCodes = Locale.getISOCountries();
for (String countryCode : countryCodes) {
Locale obj = new Locale("", countryCode);
System.out.println("Country Code = " + obj.getCountry()
+ ", Country Name = " + obj.getDisplayCountry());
}
Refer to this country list in Javafor more examples.
回答by Enamul Haque
You can use from json bellow like
您可以从 json bellow 中使用
Json parsing..
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" }]";
Or You can download full json from link : https://gist.github.com/Goles/3196253
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": "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" }]";
或者您可以从链接下载完整的 json:https: //gist.github.com/Goles/3196253