英文:
Illegal character (CTRL-CHAR, code 0) - Parsing Exception
问题
我目前遇到了以下问题:
```json
JsonParseException: 非法字符 ((CTRL-CHAR, code 0)):在标记之间只允许普通空白字符 (\r, \n, \t)
at [Source: (String)" ){"BusinessDate"
这是因为在"BusinessDate"之前有一个')'字符,并且在它之前有四个NUL字符,但是它们在这里不显示出来,因为它们是虚构的字符。这是我正在尝试映射到对象的JSON字符串的开头。我只是发送一个以Avro序列化的ByteArray,并尝试使用ByteArray Deserializer进行反序列化(我认为这是问题所在)。我如何摆脱这些虚构的字符,以便它正确地映射到我的对象,或者如何更改我的SCS(Spring Cloud Stream)消费者配置,以便在仅针对该消费者使用Avro反序列化。
// 外部主题监听器
@StreamListener(ChannelsScheduler.SCHEDULER_IN_FROM_EXTERNAL_EVENT)
public void consumeMessage(@Payload GenericMessage<String> message) throws IOException
{
logger.info("从外部主题消费消息: {}", message);
我尝试过以下方法:
GPTMStatus gptmStatus = mapper.readValue(message.getPayload(), GPTMStatus.class);
GPTMStatus gptmStatus = mapper.readValue(message.getPayload().trim(), GPTMStatus.class);
GPTMStatus gptmStatus = mapper.readValue(message.getPayload().replace(")", ""), GPTMStatus.class);
String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));
GPTMStatus gptmStatus = mapper.readValue(json, GPTMStatus.class);
所有三种方法都收到了与上述相同的错误。
完整的载荷:
" ){"BusinessDate":"2020-03-05","ContentUri":"20180712_EOB/1583443159984/0.xml","DFReference":"80712_EOB","DFRevision":0,"DFVersion":1583443159984}"
这是我发送消息的方式:
kafka
.send(
destination,
formatter.transform(df.getCanonicalPayload()).getBytes(StandardCharsets.UTF_8))
.get();
<details>
<summary>英文:</summary>
I am currently running into
JsonParseException: Illegal character ((CTRL-CHAR, code 0)): only regular white space (\r, \n, \t) is allowed between tokens
at [Source: (String)" ){"BusinessDate"
It is because of the ')' before business date and there is four NUL before that, but they don't show up here as they are imaginary characters. That is the beginning of my json string I'm trying to map to my object. I'm just sending a ByteArray serialized as Avro, and trying to Deserialize using a ByteArray Deserializer (which I'm assuming is the problem). How do I get rid of those imaginary characters so it maps correctly to my object, or change my SCS consumer config to use Avro deserialization, only on that one consumer.
// External topic listener
@StreamListener(ChannelsScheduler.SCHEDULER_IN_FROM_EXTERNAL_EVENT)
public void consumeMessage(@Payload GenericMessage<String> message) throws IOException
{
logger.info("Consumed message from external topic: {}", message);
I have tried
GPTMStatus gptmStatus = mapper.readValue(message.getPayload(), GPTMStatus.class);
GPTMStatus gptmStatus = mapper.readValue(message.getPayload().trim(), GPTMStatus.class);
GPTMStatus gptmStatus = mapper.readValue(message.getPayload().replace(")", ""), GPTMStatus.class);
String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));
GPTMStatus gptmStatus = mapper.readValue(json, GPTMStatus.class);
All three are receiving the same error as above.
Full Payload:
" ){"BusinessDate":"2020-03-05","ContentUri":"20180712_EOB/1583443159984/0.xml","DFReference":"80712_EOB","DFRevision":0,"DFVersion":1583443159984}"
This is how I'm sending the message:
kafka
.send(
destination,
formatter.transform(df.getCanonicalPayload()).getBytes(StandardCharsets.UTF_8))
.get();
</details>
# 答案1
**得分**: 1
这一行略显误导(至少对我来说)
```String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));```
JSON<b>可以</b>包含`NULL`控制字符,但仅当它被转义并且位于字符串字段内。我建议从一些简单的东西开始,比如`json.replace("
</details>
# 答案1
**得分**: 1
这一行略显误导(至少对我来说)
```String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));```
JSON<b>可以</b>包含`NULL`控制字符,但仅当它被转义并且位于字符串字段内。我建议从一些简单的东西开始,比如`json.replace("\0", "");`,然后检查一下你的代码是否可以继续运行。
<details>
<summary>英文:</summary>
This line is slightly misleading (at least to me)
```String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));```
JSON <b>CAN</b> contain the `NULL` control character but only if it's escaped and within a string field. I'd suggest starting with something simple, `json.replace("\0", "");` and checking if your code gets any further.
</details>
", "");`,然后检查一下你的代码是否可以继续运行。
<details>
<summary>英文:</summary>
This line is slightly misleading (at least to me)
```String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));```
JSON <b>CAN</b> contain the `NULL` control character but only if it's escaped and within a string field. I'd suggest starting with something simple, `json.replace("
</details>
# 答案1
**得分**: 1
这一行略显误导(至少对我来说)
```String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));```
JSON<b>可以</b>包含`NULL`控制字符,但仅当它被转义并且位于字符串字段内。我建议从一些简单的东西开始,比如`json.replace("\0", "");`,然后检查一下你的代码是否可以继续运行。
<details>
<summary>英文:</summary>
This line is slightly misleading (at least to me)
```String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));```
JSON <b>CAN</b> contain the `NULL` control character but only if it's escaped and within a string field. I'd suggest starting with something simple, `json.replace("\0", "");` and checking if your code gets any further.
</details>
", "");` and checking if your code gets any further.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论