我有一个程序可以处理客户输入并将其转换为 json 字符串,这样我就可以将该字符串作为 json 类型放入 mysql 中。我使用的库是 JSONObject
放置键/值并将其作为字符串读取。代码示例如下:
final JSONObject json = new JSONObject();
StringBuilder sb = new StringBuilder();
sb.append('\u0081');
sb.append('\u0072');
final String value = "Müller";
json.put(sb.toString(), value);
System.out.println(json);
结果是
{"\u0081r":"Müller"}
unicode char '\u0081'
被视为原始字符串,但 char '\u0072'
被正确解码为 char。我跟踪 JSONObject
中的代码,发现它具有以不同方式处理各种范围内的 unicode 字符的逻辑,c
是字符串中的字符:
if (c >= ' ' && (c < 128 || c >= 160) && (c < 8192 || c >= 8448)) {
w.write(c);
} else {
String hhhh = "000" + Integer.toHexString(c);
w.write("\\u" + hhhh.substring(hhhh.length() - 4));
}
我的问题是: