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.
Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);
JSON strings do not allow real newlines in its data; it can only have escaped newlines.
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"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With