除了Locale.getISOCountries()
之外,因为我已经在这方面出现了一些奇怪的错误。还有什么是获得两个字母的国家代码和完整国家名称的最佳方法?
获取Java国家列表的最佳方式
原标题:
最佳回答
对于一个单独的项目,我从ISO站点。
注意以下事项:
- 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.
问题回答
请参阅代码片段:
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中的国家列表以获取更多示例。
- 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
你可以像下面这样使用from 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字符串在这里
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
相关问题
热门标签
- winforms
- combobox
- fogbugz
- java
- date
- internationalization
- asp.net
- iis
- url-rewriting
- urlrewriter
- c#
- enums
- ocaml
- haxe
- algorithm
- string
- viewstate
- .net
- c++
- c
- symbol-table
- mysql
- database
- postgresql
- licensing
- migration
- vb.net
- vb6
- declaration
- vb6-migration
- python
- psycopg2
- backup
- vmware
- virtualization
- gnu-screen
- authentication
- desktop
- excel
- xll
- cultureinfo
- regioninfo
- oracle
- client
- session
- download
- html
- virtual
- constructor
- scenarios
- perl
- full-text-search
- javascript
- ajax
- testing
- oop
- inheritance
- vim
- encapsulation
- information-hiding