英文:
JSON Not getting formatted in Kafka
问题
我正在将Java对象序列化为JSON,如下所示:
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(serializableObject);
当我在控制台打印时,我可以正常看到它。
{
"topicName" : "LVS",
"sellerId" : "TS",
"orderId" : "123456",
"lineIds" : [ {
"lineId" : "1",
"sublineIds" : [ "1" ]
} ]
}
这个字符串被发送到一个Kafka主题。但是当我查看主题时,JSON显示如下。我如何避免\n和其他斜杠,并将其作为格式化的JSON发送到Kafka。
"inputPayload" : "{\"topicName\" : \"LVS\",\"sellerId\" : \"TS\",\"orderId\" : \"123456\",\"lineIds\" : [{\"lineId\" : \"1\",\"sublineIds\" : [\"1\"]}]}"
英文:
I am serializing a java object to JSON like below
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString( serializableObject );
When i am printing it in console i am seeing it properly .
{
"topicName" : "LVS",
"sellerId" : "TS",
"orderId" : "123456",
"lineIds" : [ {
"lineId" : "1",
"sublineIds" : [ "1" ]
} ]
}
this string is sending to a Kafak topic . But when i see topic the JSON showing as below . How i can avoid the \n and other slashes and get in kafka as a formatted json.
"inputPayload" : "{\n \"topicName\" : \"LVS\",\n \"sellerId\" : \"TS\",\n \"orderId\" :
\"123456\",\n \"lineIds\" : [ {\n \"lineId\" : \"1\",\n \"sublineIds\" : [ \"1\" ]\n } ]\n}"
答案1
得分: 2
数据已正确存储。您用于从Kafka查看数据的方式未将反斜杠解释为转义字符并执行格式化以供显示,这没问题。假定在您的系统中有一个进程将数据从Kafka读取并反序列化,那么对于您这里的内容,这将正常工作。
英文:
The data is getting stored properly. Whatever you're using to view it from Kafka is not interpreting the backslashes as escape characters and performing that formatting for display purposes, and that's fine. Presumably there is a process in your system that will read the data out of Kafka and deserialize it, and that will work fine with what you have here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论