【发布时间】:2020-06-07 23:14:10
【问题描述】:
我的问题可能已经在 StackoverFlow 上得到解答,但我找不到。 我的问题很简单:我通过API请求数据,返回的数据是unicode字符,例如:
"SpecialOffer":[{"title":"Offre Vente Priv\u00e9e 1 jour 2019 2020"}]
我需要将“\u00e9e”转换为“é”。 我不能做一个“replaceAll”,因为我不能提前知道所有的字符。
我试试这个:
byte[] utf8 = reponse.getBytes("UTF-8")
String string = new String(utf8, "UTF-8");
但字符串仍然有“\u00e9e”
还有这个:
byte[] utf8 = reponse.getBytes(StandardCharsets.UTF_8);
String string = new String(utf8, StandardCharsets.UTF_8);
也试过这个:
string = string.replace("\\\\", "\\");
byte[] utf8Bytes = null;
String convertedString = null;
utf8Bytes = string.getBytes("UTF8") -- Or StandardCharsets.UTF_8 OR UTF-8 OR UTF_8;
convertedString = new String(utf8Bytes, "UTF8") -- Or StandardCharsets.UTF_8 OR UTF-8 OR UTF_8;;
System.out.println(convertedString);
return convertedString;
但它也不起作用。
我测试了其他方法,但我认为我删除了所有无效的方法,因此我无法在此处向您展示。
我确信有一个非常简单的方法,但我不应该在互联网上使用正确的词汇进行搜索。你能帮帮我吗?
祝您有美好的一天,并提前非常感谢您。
【问题讨论】:
-
string.getBytes("UTF-8");而不是string.getBytes("UTF8");应该可以解决问题。 -
@Joel 感谢您的评论,我刚刚测试过,但也不起作用...:/
-
好的,先尝试将其转换为Unicode - 然后将其转换为utf8
-
@jhamon 感谢您的评论。我已经测试过这种方法,我发现很难适应我的情况。就我而言,我的字符串也可以包含数字“u”,并且可以有很多随机分布的 unicode。
标签: java string utf-8 converters