D
D
dNertyco2021-10-24 13:39:25
PHP
dNertyco, 2021-10-24 13:39:25

How do I preserve all line breaks (\n) with json_decode()?

How do I preserve all line breaks (\n) with json_decode()?
my json

{"id":0,"questionCodeSyntax":5,"questionCode":"class Test {\n func doSmth() {\n print(\"done\")\n }\n}\n\nvar test: Test?\ntest = Test()\nif let uTest = test {\n test = nil\n uTest.doSmth()\n}"}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ipatiev, 2021-10-24
@dNertyco

Newlines are perfectly preserved, and are always output exactly as they were before encoding.
If we are talking about saving newline mnemonics (\n), then nothing, of course.
You can add them back later.
But in fact, you need to ask yourself a completely different question, what does the program code do in Jason?
BUT! It seems to have gotten to me. the client renders this code in HTML as is.
And we are all on the verge of a great discovery that all beginners in PHP make for themselves - the nl2br() functions! :)
Not to mention more complex technologies like tag <pre>or functionsyntax_highlight()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question