JSON在Kafka中未被格式化。

huangapple go评论83阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年5月2日 12:30:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/61554473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定