当我得到JSON时,其中的\u003c和\u003e代替了<和>。我想在Java中将它们转换回UTF-8。任何帮助都将不胜感激。谢谢。
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) {
}
请参考http://www.exampledepot.com/egs/java.lang/unicodetoutf8.html
public static String unicodeToUTF8(String unicodeStr)
{
// 从Unicode转换为UTF-8
byte[] utf8 = unicodeStr.getBytes("UTF-8");
String UTF8Str="";
UTF8Str = new String(utf8, "UTF-8");
return UTF8Str;
} - Mubbasher KhaliqunicodeToUTF8
函数。 - Hemant MetaliaString string = "\u003c"
的测试应用程序能够工作的原因是\u003c
就像\n
一样是编译器转义。如果您想要测试JSON输入,您需要添加另一个级别的转义:String string ="\\u003c";
为了处理这些内容,您需要使用一个可以自动处理这些转义的库。您的JSON解析器应该能够做到这一点。 - bames53byte[] utfString = str.getBytes("UTF-8") ;
通过指定UTF-8编码,将其转换回字符串对象,如下所示:
str = new String(utfString,"UTF-8") ;
你也可以尝试这个
String s = "Hello World!";
String convertedInUTF8 = new String(s, StandardCharsets.US_ASCII);
<
和>
。你需要转换什么吗? - Hot LicksgetBytes
方法,但我不确定这是否真正相关。 - bobinceStringEntity
,您应该查看这个答案。 - JJD