Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将Unicode转换为UTF-8_Java_String_Utf 8_Converters - Fatal编程技术网

Java 将Unicode转换为UTF-8

Java 将Unicode转换为UTF-8,java,string,utf-8,converters,Java,String,Utf 8,Converters,我的问题可能已经在StackoverFlow上得到了回答,但我找不到它。 我的问题很简单:我通过API请求数据,返回的数据具有unicode字符,例如: "SpecialOffer":[{"title":"Offre Vente Priv\u00e9e 1 jour 2019 2020"}] 我需要将“\u00e9e”转换为“é”。 我不能做一个“替换全部”,因为我不能预先知道将要出现的所有字符 我试试这个: byte[] utf8 = reponse.getBytes("UTF-8") St

我的问题可能已经在StackoverFlow上得到了回答,但我找不到它。 我的问题很简单:我通过API请求数据,返回的数据具有unicode字符,例如:

"SpecialOffer":[{"title":"Offre Vente Priv\u00e9e 1 jour 2019 2020"}]
我需要将“\u00e9e”转换为“é”。 我不能做一个“替换全部”,因为我不能预先知道将要出现的所有字符

我试试这个:

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方法需要有效的字符集[1]

从javadoc[2]中,有效案例如下

  • US-ASCII
  • ISO-8859-1
  • UTF-8
  • UTF-16BE
  • UTF-16LE
  • UTF-16
因此,您需要在getBytes方法中使用UTF-8

[1] [2] 你可以使用小型图书馆


我没有看到的问题是,API没有返回我“\u00e9e”,而是“\\u00e9e”,因为它是一个字符序列,而不是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();
inti=0,len=s.length();
字符c;
StringBuffer sb=新的StringBuffer(len);
而(我
在此处找到此解决方案:
string.getBytes(“UTF-8”)
而不是
string.getBytes(“UTF8”)
应该可以。@Joel谢谢你的评论,我刚刚测试过,但也不起作用…:/这回答了你的问题吗?好的,首先尝试将其转换为Unicode,然后再转换utf8@jhamon谢谢你的评论。我已经测试过这个方法,我发现很难将它应用到我的案例中。在我的例子中,我的字符串也可以包含数字“u”,并且可以有很多随机分布的Unicode。顺便说一下,UTF-8是一种特殊的Unicode编码方式谢谢你的回复。我修改了我的问题,所以我指定我也使用StandardCharsets.UTF_8和“UTF-8”进行测试,但这两种测试都不起作用。您也可以使用Commons Lang库,正如@jhamon报告的问题中所述
String text = "Offre Vente Priv\\u00e9e 1 jour 2019 2020";
System.out.println(" result: " + JsonEscaper.unescape(text));
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();