【问题标题】:Convert Unicode to UTF-8将 Unicode 转换为 UTF-8
【发布时间】: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


【解决方案1】:

String.getBytes 方法需要有效的 Charset [1]

来自 javadoc [2] 的有效案例是

  • US-ASCII
  • ISO-8859-1
  • UTF-8
  • UTF-16BE
  • UTF-16LE
  • UTF-16

所以你需要在getBytes方法中使用UTF-8。

[1]https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#getBytes-java.nio.charset.Charset- [2]https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html

【讨论】:

  • 顺便说一下,UTF-8 是一种特殊的 Unicode 编码方式
  • 感谢您的回复。我修改了我的问题,所以我指定我也用 StandardCharsets.UTF_8 和“UTF-8”进行了测试,它也不起作用
  • 您还可以使用 @jhamon 报告的问题中报告的 Commons Lang 库
【解决方案2】:

你可以使用小的json

String jsonstring = "{\"SpecialOffer\":[{\"title\":\"Offre Vente Priv\\u00e9e 1 jour 2019 2020\"}]}";
JsonValue json = JsonParser.parse(jsonstring);
String value = json.asObject()
    .first("SpecialOffer").asArray().get(0)
    .asObject().first("title").asStringLiteral().stringValue();
System.out.println(" result: " + value);

String text = "Offre Vente Priv\\u00e9e 1 jour 2019 2020";
System.out.println(" result: " + JsonEscaper.unescape(text));

【讨论】:

    【解决方案3】:

    我没有看到的问题是 API 没有返回“\u00e9e”而是“\\u00e9e”,因为它是字符序列而不是 unicode 字符! 所以我必须重新创建所有的 unicode,一切正常!

    int i=0, len=s.length();
            char c;
            StringBuffer sb = new StringBuffer(len);
            while (i < len) {
                c = s.charAt(i++);
                if (c == '\\') {
                    if (i < len) {
                        c = s.charAt(i++);
                        if (c == 'u') {
                            // TODO: check that 4 more chars exist and are all hex digits
                            c = (char) Integer.parseInt(s.substring(i, i+4), 16);
                            i += 4;
                        } // add other cases here as desired...
                    }
                } // fall through: \ escapes itself, quotes any character but u
                sb.append(c);
            }
            return sb.toString();
    

    在此处找到此解决方案: Java: How to create unicode from string "\u00C3" etc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 2015-10-08
      • 2023-03-12
      • 2016-08-24
      • 1970-01-01
      • 2017-06-20
      • 2011-06-05
      相关资源
      最近更新 更多