在使用hbase shell等命令时,若输出的内容包含中文,经常会出现乱码等现象,我们可以在命令后面加上 {formatter => 'tostring'}来处理 例如:
scan 'test', {formatter => 'tostring'}那么,在java中如何来解析这些字符串,使之能正常显示中文呢?可以参考下面的代码:
带解析字符串为:
{
"first_name":"\xE4\xB8\x89\xE5\x8F\xB6\xE4\xB8\x9C\xE8\xB7\xAF",
"second_name":"\xE4\xB8\x89\xE5\x8F\xB6\xE4\xB8\x9C\xE8\xB7\xAF"
}
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo {
private static final Pattern UNICODE_PATTERN = Pattern.compile("\\\\x([0-9a-fA-F]{2})");
public static String decodeJson(String json) {
Matcher matcher = UNICODE_PATTERN.matcher(json);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
char c = (char) Integer.parseInt(matcher.group(1), 16);
matcher.appendReplacement(sb, String.valueOf(c));
}
matcher.appendTail(sb);
return new String(sb.toString().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
}
public static void main(String[] args) throws UnsupportedEncodingException {
String json = "{\"first_name\":\"\\xE4\\xB8\\x89\\xE5\\x8F\\xB6\\xE4\\xB8\\x9C\\xE8\\xB7\\xAF\",\"second_name\":\"\\xE4\\xB8\\x89\\xE5\\x8F\\xB6\\xE4\\xB8\\x9C\\xE8\\xB7\\xAF\"}";
String decodedStr = decodeJson(json);
System.out.println(decodedStr); // 输出解码后的字符串
}
}
输出结果为:
{
"first_name": "三叶东路",
"second_name": "三叶东路"
}