JSON and multiple lines

Asked

Viewed 1,528 times

2

I’m building a file JSON where I intend to add several lines, as shown in the code exemplified below:

[  
   {  
      "Numero":"0001",
      "Textos":[  
         {  
            "letra":"Meu Jesus maravilhoso és,
                    minha inspiração a prosseguir,
                    e mesmo quando tudo não vai bem
                    eu continuo olhando para ti...",       
            "titulo":"Aos pés da cruz"
         }
      ]
   }
]

However, in doing so, I always get the message of invalid characters. From what I’ve researched, JSON does not support multi-line usage. Still, there is some way to create the template reported in the above code?

  • 2

    In what programming language are you trying? Could you post an example?

1 answer

2

Yes, it is possible, I know two options to do this, the first would be to use an array of words:

[  
   {  
      "Numero":"0001",
      "Textos":[  
         {  
            "letra": [
                       "Meu Jesus maravilhoso és,",
                       "minha inspiração a prosseguir,",
                       "e mesmo quando tudo não vai bem,",
                       "eu continuo olhando para ti..."       
                     ],
             "titulo":"Aos pés da cruz"
         }
      ]
   }
]

The above solution interprets multiple lines at assembly time.

Already according to the specified default you can use the following values for the type char:

  • \" - double quote
  • \\ - escape character
  • \/ - bar
  • \b - decrees the cursor pointer in a character
  • \f - page break
  • \n - line breaking
  • \r - decreases the cursor pointer in a row
  • \t - paragraph break
  • \u - four hexadecimal digits

Then you can use the \n at the end of each line so that this is interpreted as:

[  
   {  
      "Numero":"0001",
      "Textos":[  
         {  
            "letra":"Meu Jesus maravilhoso és, \n minha inspiração a prosseguir, \n e mesmo quando tudo não vai bem \n eu continuo olhando para ti...",       
            "titulo":"Aos pés da cruz"
         }
      ]
   }
] 

References:

Specification of JSON (English)

How to code long JSON value strings as multiline?

Browser other questions tagged

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