Problem using paragraphs in JSON file

Asked

Viewed 10,238 times

6

I’m creating a web application that uses JSON files to save site content.

In these files are saved small fragments of text containing paragraphs. When I try to separate the paragraph using a \n\ or \n or \r\ or \r, I don’t have the line break and if use the \n\ or \r\ the JSON file is not loaded.

What the right way to put line breaks to separate paragraphs into JSON files?

I’ve tested HTML code as <p> or a <br/> but that wouldn’t work well on Alerts.

JSON file:

{
  "pt":{
    "title":"Quem é Ohm Reaction",
    "article":"hadouken!\\n asda"
    }
}
  • 1

    This seems to be a problem of formatting the code when displaying it instead of a problem with the JSON format. How is the code you use to display it?

  • when I used it he brought the n as if it were part of the text, the n it really jumps to the next line... my mistake was that when I inserted the json in the div it showed the space but for the HTML n is represented as at most a space between characters... my solution will be to convert in the input

4 answers

7

The use of double bars (\\n, \\r etc) is usually only necessary when dealing with a string representing another string:

var texto1 = "foo\nbar";    // foo
                            // bar
var texto2 = "'foo\nbar'";  // 'foo
                            // bar'
var texto3 = "'foo\\nbar'"; // 'foo\nbar'

This is the same reason why languages without a literal for regular expressions (like Java) - or functions/constructs that create a regex from a string - need double bars:

var regex1 = /(.)\1/;              // Regex: (.)\1
                                   // Valida "xx": sim
var regex2 = new RegExp("(.)\1");  // Regex: (.) 
                                   // Valida "xx": não
var regex3 = new RegExp("(.)\\1"); // Regex: (.)\1
                                   // Valida "xx": sim

In the case of a JSON contained in a string, therefore, it is required:

var json1 = '{ "texto":"foo\nbar" }';  // { "texto":"foo
                                       // bar" }
var json2 = '{ "texto":"foo\\nbar" }'; // { "texto":"foo\nbar" }

But if this JSON was read from a file, no, because the backslash will be included directly in the string (and not interpreted in any way as an escape character).

I know this does not directly answer your question (since it has already been determined in the comments that your problem was at the time of displaying text using HTML), but I hope it helps to illustrate why in some situations it is used \n and others \\n: in the first you are "escaping" the n, in the other you are escaping own \ (and maintaining the n intact).

  • thanks for the information, all knowledge is welcome.

3


Just one \n simple in the final json string.

>>> var pessoa = { nome: 'James Bond', apresentacao: 'Meu nome é Bond...\n\nJames Bond.' };  
>>> console.log(pessoa.apresentacao);
Menu nome é Bond...

James Bond.
  • 3

    I would just like to point out that your example is not JSON, is simply a common Javascript object. JSON does not accept single quotes, nor does it allow attribute names to be expressed without quotation marks. Confusion is common, given the similarity between the format JSON and the literal for objects Javascript. The first was inspired by the second, and in fact you can do eval in a JSON and the result will always be a valid object. The reciprocal is not true (not every literal for JS object will be considered valid JSON).

1

Assuming you are using quotes in your JSON file, use \\n for line breaking.

The JSON format requires certain special characters, such as line break, to be "escaped" using \. Therefore, to produce the string \n, you must escape the bar \ using another bar before.

  • My JSON file:{"pt":{"title":"Quem é Ohm Reaction","article":"hadouken!\\n asda"}} even so n does not solve. it is called and shown on the screen as "hadouken! n"

  • I can’t do line breaking either, does anyone have a solution? I tried with two bars too, but it wasn’t.

-4

Put n where you want to break the line, then use a Slice to slice these parts so you have multiple elements in your array and can then break into rows.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.