JAVA中JSON解析与控制字符的问题
我通过 HTTP
收到 JSON 请愿书。当来自 Internet Explorer 8
时,解析失败并出现异常:
InPart inPart = mp.next();
MyClass myClass = inPart.getBody(MyClass.class, null);
com.ctc.wstx.exc.WstxUnexpectedCharException: Illegal character ((CTRL-CHAR, code 12)) at [row,col {unknown-source}]: [1,101]]
相关代码:
InPart inPart = mp.next();
String s = inPart.getBody(String.class, null);
providers.getMessageBodyReader(MyClass.class, null, null,
MediaType.APPLICATION_JSON_TYPE).readFrom(MyClass.class, null, null,
MediaType.APPLICATION_JSON_TYPE, headers.getRequestHeaders(),
new ByteArrayInputStream(s.getBytes())); // Tried with s.getBytes("UTF-8")
com.ctc.wstx.exc.WstxUnexpectedCharException: Illegal character ((CTRL-CHAR, code 12)) at [row,col {unknown-source}]: [1,101]
此外,如果我这样做:
String ss = s.replaceAll("\\p{Cntrl}", "");
ss.equals(s); //
输出真实
长度是相同的。
我也尝试过:
private String removeControlChar(String in) {
StringBuilder sb = new StringBuilder();
for (char c : in.toCharArray())
{
if(!Character.isISOControl(c)) {
sb.append(c);
}
else
{
// To delete
int i = 0;
}
}
return sb.toString();
}
InPart inPart = mp.next();
String s = inPart.getBody(String.class, null);
Strign ss = removeControlChar(s);
providers.getMessageBodyReader(MyClass.class, null, null,
MediaType.APPLICATION_JSON_TYPE).readFrom(MyClass.class, null, null,
MediaType.APPLICATION_JSON_TYPE, headers.getRequestHeaders(),
new ByteArrayInputStream(ss.getBytes())); // Tried with s.getBytes("UTF-8")
如果我调试,失败的字符是异常中所述的 \f
。但错误表明它是无效的 XML 字符。这可能是问题所在吗?
有什么想法吗?这似乎只影响 Internet Explorer。
谢谢。
I'm receiving via HTTP
a JSON petition. When coming from Internet Explorer 8
parsing fails with the exception:
InPart inPart = mp.next();
MyClass myClass = inPart.getBody(MyClass.class, null);
com.ctc.wstx.exc.WstxUnexpectedCharException: Illegal character ((CTRL-CHAR, code 12)) at [row,col {unknown-source}]: [1,101]]
The relevant code:
InPart inPart = mp.next();
String s = inPart.getBody(String.class, null);
providers.getMessageBodyReader(MyClass.class, null, null,
MediaType.APPLICATION_JSON_TYPE).readFrom(MyClass.class, null, null,
MediaType.APPLICATION_JSON_TYPE, headers.getRequestHeaders(),
new ByteArrayInputStream(s.getBytes())); // Tried with s.getBytes("UTF-8")
com.ctc.wstx.exc.WstxUnexpectedCharException: Illegal character ((CTRL-CHAR, code 12)) at [row,col {unknown-source}]: [1,101]
Furthermore if I do:
String ss = s.replaceAll("\\p{Cntrl}", "");
ss.equals(s); //
Outputs a true
lengths are the same.
I also tried:
private String removeControlChar(String in) {
StringBuilder sb = new StringBuilder();
for (char c : in.toCharArray())
{
if(!Character.isISOControl(c)) {
sb.append(c);
}
else
{
// To delete
int i = 0;
}
}
return sb.toString();
}
InPart inPart = mp.next();
String s = inPart.getBody(String.class, null);
Strign ss = removeControlChar(s);
providers.getMessageBodyReader(MyClass.class, null, null,
MediaType.APPLICATION_JSON_TYPE).readFrom(MyClass.class, null, null,
MediaType.APPLICATION_JSON_TYPE, headers.getRequestHeaders(),
new ByteArrayInputStream(ss.getBytes())); // Tried with s.getBytes("UTF-8")
If I debug, the character which fails is a \f
as stated in the exception. But the error says it is an invalid XML character. Might this be the problem?
Any Ideas? This only seems to affect Internet Explorer.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会删除这个问题,以防万一有人遇到同样的麻烦。
问题是浏览器发送像
C:\fakepath\
这样的路径,在执行时,转换为 XML 后它会显示\f
。您可以在浏览器中设置此行为。
问候。
I don't delete this question just in case anyone fall in the same trouble.
The problem is the browser sending the path like
C:\fakepath\
which in execution time, and after converting to XML it states\f
.You can set up this behavior in you browser.
Regards.