在java中将UTF-16 unicode字符转换为UTF-8

时间:2023-01-05 16:25:53

When I got JSON then there are \u003c and \u003e instead of < and >. I want to convert them back to utf-8 in java. any help will be highly appreciated. Thanks.

当我得到JSON时,有\u003c和\u003e而不是 <和> 。我想用java把它们转换回utf-8。非常感谢您的帮助。谢谢。

3 个解决方案

#1


9  

try {
    // Convert from Unicode to UTF-8
    String string = "\u003c";
    byte[] utf8 = string.getBytes("UTF-8");

    // Convert from UTF-8 to Unicode
    string = new String(utf8, "UTF-8");
} catch (UnsupportedEncodingException e) {
}

refer http://www.exampledepot.com/egs/java.lang/unicodetoutf8.html

请参阅http://www.exampledepot.com/egs/java.lang/unicodetoutf8.html

#2


0  

There are a couple of answers already out there: How to convert Strings to and from UTF8 byte arrays in Java

已经有几个答案了:如何在Java中将字符串转换为UTF8字节数组

Good luck, M

祝你好运,米

#3


0  

You can try converting the string into a byte array

您可以尝试将字符串转换为字节数组

byte[] utfString = str.getBytes("UTF-8") ;

and convert that back to a string object by specifying the UTF-8 encoding like

并通过指定UTF-8编码将其转换回字符串对象

str = new String(utfString,"UTF-8") ;

#1


9  

try {
    // Convert from Unicode to UTF-8
    String string = "\u003c";
    byte[] utf8 = string.getBytes("UTF-8");

    // Convert from UTF-8 to Unicode
    string = new String(utf8, "UTF-8");
} catch (UnsupportedEncodingException e) {
}

refer http://www.exampledepot.com/egs/java.lang/unicodetoutf8.html

请参阅http://www.exampledepot.com/egs/java.lang/unicodetoutf8.html

#2


0  

There are a couple of answers already out there: How to convert Strings to and from UTF8 byte arrays in Java

已经有几个答案了:如何在Java中将字符串转换为UTF8字节数组

Good luck, M

祝你好运,米

#3


0  

You can try converting the string into a byte array

您可以尝试将字符串转换为字节数组

byte[] utfString = str.getBytes("UTF-8") ;

and convert that back to a string object by specifying the UTF-8 encoding like

并通过指定UTF-8编码将其转换回字符串对象

str = new String(utfString,"UTF-8") ;