原因是发消息的时候用的是json-20160810.jar的JSONObject,里面有一些特殊字符特殊处理的逻辑,
public static Writer quote(String string, Writer w) throws IOException {
if (string != null && string.length() != 0) {
char c = 0;
int len = string.length();
w.write(34);
for(int i = 0; i < len; ++i) {
char b = c;
c = string.charAt(i);
switch(c) {
case '\b':
w.write("\\b");
continue;
case '\t':
w.write("\\t");
continue;
case '\n':
w.write("\\n");
continue;
case '\f':
w.write("\\f");
continue;
case '\r':
w.write("\\r");
continue;
case '"':
case '\\':
w.write(92);
w.write(c);
continue;
case '/':
if (b == '<') {
w.write(92);
}
w.write(c);
continue;
}
// 这里》》》特殊逻辑
if (c >= ' ' && (c < 128 || c >= 160) && (c < 8192 || c >= 8448)) {
w.write(c);
} else {
w.write("\\u");
String hhhh = Integer.toHexString(c);
w.write("0000", 0, 4 - hhhh.length());
w.write(hhhh);
}
}
w.write(34);
return w;
} else {
w.write("\"\"");
return w;
}
}
其中双引号的ASCII如下:
代码:
String s = "“”";// 字符串
char[] chars = s.toCharArray();
System.out.println("\n\n汉字 ASCII\n----------------------");
for (int i = 0; i < chars.length; i++) {// 输出结果
System.out.println(" " + chars[i] + " " + (int) chars[i]);
}
输出结果:
汉字 ASCII
----------------------
“ 8220
” 8221
很明显8220和8221都是走else特殊逻辑,所以在微信显示会是\u***这种格式的。。。
以前不知所以然,总以为微信解析不了中文字符双引号,所以一直用英文双引号代替。
现在如果要正确使用得改为其他json解析包,比如Gson,这个改动有点大,有时间才行。
微信发送中文双引号出现无法解析的问题,根源在于使用的json-20160810.jar的JSONObject处理特殊字符时的逻辑。由于8220和8221ASCII码被特殊处理,导致在微信中显示为u***格式。解决方法是更换为如Gson等其他JSON解析库,但该改动可能涉及较大工作量。

2769

被折叠的 条评论
为什么被折叠?



