JSON解析错误 - 无效的UTF-8起始字节0xa0。

4
我在向服务器发送JSON数据时遇到了问题。我猜测这是与UTF-8格式开头不希望出现的坏字符有关。
我使用了CharDecoder来替换所有畸形的UTF-8字符,以下是代码。
 // Construct the Decoder
    CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder();
    utf8Decoder.onMalformedInput(CodingErrorAction.REPLACE);
    utf8Decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    //  Configure to replace Malformed input with space
    utf8Decoder.replaceWith(" ");

    //  Construct ByteBuffer
    ByteBuffer byteBuff = ByteBuffer.wrap(text.getBytes());
    try {
        //  Process the text.
        CharBuffer parsed = utf8Decoder.decode(byteBuff);
        return new String(parsed.array());
    } catch (CharacterCodingException e) {
        e.printStackTrace();
    }

这并没有帮助到我。当我查看Json post数据中解析器出错的列行时,发现是一个空格字符。

Json转换为post请求为

{"body":{"messageSegments":[{"type":"Text","text":"This is a link "},{"type":"Mention","id":"005GGGGGG02g6MMIAZ"},{"type":"Text","text":" ish"}]},"capabilities":{"questionAndAnswers":{"questionTitle":"https:\/\/www.google.co.nz\/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-16"}}}

错误是

[{"errorCode":"JSON_PARSER_ERROR","message":"在[line:1, column:139]处出现无效的UTF-8起始字节0xa0"}]

有什么线索吗?

谢谢,
Sree


你正在获取解析响应的错误。您可以使用Volley和Gson调用Rest API。这里有一个类似的例子 http://stackoverflow.com/a/37242140/3073945 - Md. Sajedul Karim
我正在使用Salesforce移动SDK,所以我没有自由使用其他NW库。但是Salesforce内部正在使用Volley库。谢谢。 - sha
请点击此链接查看:https://dev59.com/M3_aa4cB1Zd3GeqP4owx - RejoylinLokeshwaran
2个回答

2

0xa0是Unicode字符'NO-BREAK SPACE'(U+00A0)

不是通常的空格0x20

在视觉上很难注意到区别,但某些框架不喜欢它。


1
请注意,UTF-8中的“NO-BREAK SPACE”是两个字节:0xc2 0xa0。请参考以下链接:https://www.utf8-chartable.de/unicode-utf8-table.pl?utf8=0x 如果无法找到纯粹的0xA0在UTF-8中,那么这也是有道理的,因为它不是有效的起始字节。 - Jason Young

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接