I'm using JSON to write some data file formats, and want to keep some very long string values into multiple lines. Python module using the JSON I get a lot of mistakes, no matter what I use \\
or \\n
as an escape.
Can you use a multi-line strings in JSON? This is mainly for the comfort of visual, so I think I can turn wrap my editor, but I'm just curious ......
#1st Floor
This is a very old problem, but I encountered this problem when searching, I think I know the source of the problem.
JSON does not allow the use of their data in "real" line breaks; it can only escape wrap. See @YOU the answer . According to this problem, it looks like you tried to escape in two ways newline in Python: Using the line continuation character ( "\\"
) or use "\\n"
as an escape.
But remember: If you use a string, a special escape character in python ( "\\t"
, ) "\\n"
will be converted to REAL control characters! "\\n"
The replacement is a newline ASCII control character, which happens to be in JSON illegal characters. (As for line continuation character, it only needs to wrap.)
So you need to do is to prevent the escape character Python. You can use the original string (to r
be placed before the string, such as r"abc\\ndef"
, adding an additional or a slash before newline ( "abc\\\\ndef"
) to do this.
The above two methods will not "\\n"
replace a true newline ASCII control character, but rather "\\n"
as two characters in the text, then JSON can be interpreted as a line break.
#2nd Floor
View specifications ! JSON grammar char generation may take the following values:
- any-Unicode-character-except-
"
-or-\\
-or-control-character -
\\"
-
\\\\
-
\\/
-
\\b
-
\\f
-
\\n
-
\\r
-
\\t
-
\\u\u003c/code> 四个十六进制数字
换行符是“控制字符”,所以不,你的字符串中可能没有文字换行符。 但是,您可以使用
\\n
和\\r
\\n
所需的任何组合对其进行编码。
#3rd floor
The multiple lines of text on the txt file and then
var str = {
text: cat('path_to_file/text.txt')
}
(It runs on mongoDB)
#4th floor
JSON does not allow real line breaks. You need \\n
to replace all line breaks.
E.g:
"first line second line"
You can save:
"first line\\nsecond line"
note:
For Python
, this should be written as:
"first line\\\\nsecond line"
Which \\\\
is used to escape the backslash, otherwise python will \\n
as a control character "new line"
#5th Floor
Using regular expressions will all appear \\r\\n
replaced \\\\n
.
This is useful to me in the scala.
val newstr = str.replace("\r\n", "\\n")
#6th floor
JSON does not allow line breaks to improve readability.
The best way is to use a IDE you can wrap.
#7th floor
The property value is written as a string array. Like the examples given here https://gun.io/blog/multi-line-strings-in-json/ . This will help.
We can always use an array of strings as multi-line strings, as shown below.
{
"singleLine": "Some singleline String",
"multiline": ["Line one", "line Two", "Line Three"]
}
We can easily display the contents of an array of iterations in a multi-line mode.
Building # 8
Can you use a multi-line strings in JSON?
Yes. I just use my Firefox web browser to test this out, press F12, at the bottom of the screen and click on the console input.
x={text:"hello\nworld"}
Just from an object x contains JSON format string to create a multi-line strings.
console.log(x.text)
hello
world
Display x.text, which is a multi-line display string.
Both tests show that, Firefox's Javascript interpreter will be happy to create a multi-line strings and use JSON.
Use JSON.stringify
and JSON.parse
conduct more tests show, Javascript interpreter can contain multiple lines of an object into a JSON string and parse it again, absolutely no problem.
I used to store all the works of Shakespeare as a JSON object properties, and then sent over the Internet, there is no damage.
Case
This is two lines of character strings input through the three lines
x={text:"expert\
s\nex\
change"}
We can display objects
console.log(x)
give
Object { text: "experts\nexchange" }
Or string
console.log(x.text)
give
experts
exchange
String from the end of the row end use \\ n, and the line using only the input line to achieve a plurality \\.
In fact, you may want to synchronize with the end of the line end of the string in a row, for example,
x={text:"experts\n\
exchange"}
Multi-line string length
console.log("Hello\nWorld".length)
11
console.log("Hello World".length)
11
Please note that the string with line breaks no longer than the string with spaces. Even if two characters typed on the keyboard ( '\\' and 'n'), the string is stored only one character.
House # 9
Use json5 (loader) See https://json5.org/ - Example (by json5)
{
lineBreaks: "Look, Mom! \
No \\n's!",
}
#10th floor
Try using base64
a string encoded values. Most of the time working for me.
{
"singleLine": "Some singleline String",
"multiline": ["Line one", "line Two", "Line Three"]
}
In the base64
string encoded looks like
{
"singleLine": "Some singleline String",
"multiline": "TGluZSBvbmUKTGluZSBUd28KTGluZSBUaHJlZQ=="
}
base64 encoding and decoding available in all languages.
House # 11
Unfortunately, many of the answers here are resolved how to add a line break in the string of data problems. The question is how to split a string code values in a plurality of lines to make the code look better. (Even aware of this answer also provides a "solution" Suppose a person is free to change the data said that in many cases people do not do.)
Worse, there is no good answer.
In many programming languages, even if they are not explicitly support the interbank split the string, you can still use string concatenation to obtain the desired effect; as long as the compiler is not very bad, that's good.
But json is not a programming language; it's just a data representation. You can not tell it to the connection string. It's (relatively small) syntax does not include any tools represent strings on multiple lines for.
No design some type of pre-processor (And, for one, I think it can not effectively write my own language to solve the problem), there is no general solution to this problem. If you can change the data format, you can replace the array of strings. Otherwise, this is one of many ways json is not designed for human readability.
House # 12
If you prefer to use Node.js, you can do this:
module.exports = {
multilineStr: `
dis my life
it's now or never
`
}
You can use Node.js import and easily convert it to JSON:
echo `node -pe 'JSON.stringify(require("./json-mod.js"))'`
You get:
{"multilineStr":"\n \n dis my life\n it's now or never\n \n "}
How it works : You use Node.js load a JS module, it creates a JS object that can easily be Node.js string of. -e option assessment string -p
option will return a final result of the operation of Node.js echoed to stdout.
If you want to load .js script located in different working directory, you must switch "" and "" string:
my_script='../json-mod.js'
echo `node -pe "JSON.stringify(require('$my_script'))"`
House # 13
I have to do this for a small Node.js project, and found this solution :
{
"modify_head": [
"<script type='text/javascript'>",
"<!--",
" function drawSomeText(id) {",
" var pjs = Processing.getInstanceById(id);",
" var text = document.getElementById('inputtext').value;",
" pjs.drawText(text);}",
"-->",
"</script>"
],
"modify_body": [
"<input type='text' id='inputtext'></input>",
"<button onclick=drawSomeText('ExampleCanvas')></button>"
],
}
This looks really neat for me, because I have to use double quotes around. Although not, but I might use YAML, but there are other pitfalls and does not support. After parsing, I only use myData.modify_head.join('\\n')
or myData.modify_head.join()
, depending on whether I want to wrap after each string.