English 中文(简体)
获取Java国家列表的最佳方式
原标题:
  • 时间:2009-04-03 01:18:53
  •  标签:

除了Locale.getISOCountries()之外,因为我已经在这方面出现了一些奇怪的错误。还有什么是获得两个字母的国家代码和完整国家名称的最佳方法?

最佳回答

对于一个单独的项目,我从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中的国家列表以获取更多示例。

  1. Create a Map out of this page http://www.theodora.com/country_digraphs.html
  2. Save it to a file (I suggest XMLEncoder/XMLDecoder class)
  3. 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.
  4. Repeat (or use a bi-directional map) these steps for each column of the table on the afore mentioned webpage.
  5. 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

  1. 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();
    }
    
  2. 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"
      }]";
    
  3. 或者,您可以从以下链接下载完整的json:https://gist.github.com/Goles/3196253





相关问题
热门标签