[英]Unicode to String in java but tricky
我正在使用其API從網站獲取數據,該API以JSON
格式返回數據。 問題是當JSON
有一些變音符號時。 這將返回其UNICODE
,對於如Münich
將是Mu\̈nich
。
當我將此JSON
字符串傳遞給org.codehaus.jettison.json.JSONObject
的構造函數時, Mu\̈nich
被轉換為Munich (n has an umlaut)
。 錯誤。
我很晚才意識到這一點(在獲取了全部數據之后)。 現在,我使用以下方法將其轉換回Unicode
格式,即,我將Munich (n has an umlaut)
傳遞給該方法,並返回Mu\̈nich
。
我想這在某種程度上轉化Mu\̈nich
到Münich
。 有任何想法嗎?
請注意,僅在將u\̈
轉換為ü
,將o\̈
為ö
,將a\̈
為ä
a\̈
需要進行轉換。
用於轉換的方法-
public static String escapeUnicode(String input) {
StringBuilder b = new StringBuilder(input.length());
Formatter f = new Formatter(b);
for (char c : input.toCharArray()) {
if (c < 128) {
b.append(c);
} else {
f.format("\\u%04x", (int) c);
}
}
return b.toString();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.