Saving strings in values ​​in JSON key-value pairs

I'm not sure if line breaks are allowed in JSON values. I cannot create the following in JSON

{"foo": "I am not sure if line breaks are allowed in JSON values. I certainly am unable to create the following in JSON"} 

The following, of course, do not work

 {"foo": "I am not sure if line breaks are\nallowed in JSON values. I certainly\nam unable to create the following in JSON"} 

Preamble: I want to send a long message, as indicated above, to a browser or console application and display it neatly formatted so that it is readable to the user.

+6
source share
2 answers

If you display (or paste) the json value directly in HTML, you cannot use it the way it happens, because in html newlines are ignored and replaced with a space.

For example, if you have:

 <p>Hello, I'm in other line.</p> 

It will be presented as:

Hi, I'm on a different line.

You must convert newlines to paragraph or <br> , for example:

 <p>Hello,<br> I'm in other line.</p> 

This will be shown as:

Hello,

I'm on a different line

If this is your case, you can simply use String.replace to change \n to <br>\n .

+1
source

If you display it in HTML format, just do it. Either in the text area or something else, try "\ r \ n" wrapped in double quotes. The double backslash should run.

0
source

Source: https://habr.com/ru/post/901474/


All Articles