2016-11-11 81 views
-4

假設我的國家名稱是USA,所以我想轉換爲USD.I只有web服務的國家名稱。如何將國家名稱轉換爲貨幣代碼?

我試過這段代碼,但是如何將國家名稱轉換成Locale對象。有人可以提出這個問題嗎?

String countryName=jsonObject.getString("country"); 
+0

語言環境需要一個語言代碼作爲參數的構造函數,而不是國名:https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html#Locale(java。 lang.String) – 1615903

+0

只要在你的代碼中指出問題 - 爲什麼你會期望它的工作,因爲它明確記錄它不會? – 1615903

+0

好吧現在告訴如何解決 –

回答

1

的您也可以使用https://github.com/TakahikoKawasaki/nv-i18n庫,這是專門爲此設計的。

從它的GitHub頁面,

包,支持國際化,包含ISO 3166-1國家 代碼枚舉具體到這一點,ISO 639-1語言代碼枚舉等

例問題

String countryName = jsonObject.getString("country"); 
List<CurrencyCode> codes = CurrencyCode.getByCountry(countryName, false) 

簡單的例子來看看可用列表(從GitHub頁面),

// List all the currency codes. 
for (CurrencyCode code : CurrencyCode.values()) 
{ 
    System.out.format("[%s] %03d %s\n", code, code.getNumeric(), code.getName()); 
} 

// List all the country codes. 
for (CountryCode code : CountryCode.values()) 
{ 
    System.out.format("[%s] %s\n", code, code.getName()); 
} 
3

提供國家代碼,而不是國名

public static void main(String[] args) throws InterruptedException { 
Map<String, String> countries = new HashMap<>(); 
for (String iso : Locale.getISOCountries()) { 
    Locale l = new Locale("", iso); 
    countries.put(l.getDisplayCountry(), iso); 
} 

System.out.println(countries.get("Switzerland")); 
System.out.println(countries.get("Andorra")); 
System.out.println(countries.get("Japan")); 
} 
+0

其有用答案 –

相關問題