0

I'm creating a .json file from C# code using the JsonConvert.SerializeObject(User) and System.IO.File.WriteAllText functions. One of the user attributes is "CONFIG" which is a long multi-line string that is fetched from a DataBase. When opening the new .json file, the "CONFIG" attribute is written on a one really long line with multiple "\n" symbols. How can I change it in order to actually see the string in a multi-line string?

I tried replacing all "\n" for "\n" but it did not change anything. I also print the string on the console just before serializing it and it is printed on multiple lines.

How I want it to be :

...
{
   "USER":1,
   "SETTINGS": "SETT_1=YES
                SETT_2=YES
                SETT_3=YES"
}
...

How it is right now:

...
{
   "USER":1,
   "SETTINGS": "SETT_1=YES\nSETT_2=YES\nSETT_3=YES"
}
...
  • 2
    `"\n"` **is** an actual new line. In order to display it in your editor with line breaks, you will need to parse the string. – Code-Apprentice Jul 16 '19 at 20:22
  • 1
    That string *does* contain a new line. `\n` is a newline escape sequence. –  Jul 16 '19 at 20:24
  • 1
    See https://stackoverflow.com/questions/42068/how-do-i-handle-newlines-in-json –  Jul 16 '19 at 20:25
  • Possible duplicate of [Multiline strings in JSON](https://stackoverflow.com/questions/2392766/multiline-strings-in-json) – Code-Apprentice Jul 16 '19 at 20:26
  • 3
    Note that the JSON escape rules *require* the newline to be escaped as `\n` in the string; you cannot have an actual, unescaped newline in it (without breaking it, that is). Once parsed as a value, you can display it however you like. (You could also consider a more appropriate serialization format, where you split the actual settings into an object with properties.) – Jeroen Mostert Jul 16 '19 at 20:26
  • This I understand. But what could I do in order to actually see the new lines when I open a text editor with the resulting .json file? Thank you – LouisPopovic Jul 17 '19 at 12:40

0 Answers0