Json object containing new line character gets converted to "\n" while converting json object to string or byte. If I choose any one of the below way it is converting new line character to \n.
byte[] json = objectMapper.writeValueAsBytes(jsonObject);
String json = objectMapper.writeValueAsString(jsonObject);
objectMapper.writeValue(json, jsonElection);
Example:
Brown
And
Ken
Above data got changed as
Brown\nAnd\nKen
Help me to solve this issue.
解决方案
That is the intended working. Newline characters are escaped to the "\n" character sequence.
Your output is perfectly fine and it means the 3 words "Brown", "And" and "Ken" separated by newline characters. When you decode it you will have the exact same text (as your input).
You asked for json text and that is what you got. If a json value contains a newline character, it is represented by the character sequence "\n".