Multiline strings in JSON

2018-12-31 16:25发布

I'm writing some data files in JSON format and would like to have some really long string values split over multiple lines. Using python's JSON module I get a whole lot of errors, whether I use \ or \n as an escape.

Is it possible to have multi-line strings in JSON? It's mostly for visual comfort so I suppose I can just turn word wrap on in my editor, but I'm just kinda curious...

19条回答
永恒的永恒
2楼-- · 2018-12-31 17:02

Just include paragraph tags to make it look neat and more efficient.

Example: "body": "

Paragraph 1 here.

Paragraph 2 here.

",

查看更多
高级女魔头
3楼-- · 2018-12-31 17:07

Use regex to replace all occurrences of \r\n with \\n.

This worked for me in scala.

val newstr = str.replace("\r\n", "\\n")
查看更多
听够珍惜
4楼-- · 2018-12-31 17:07

for multiline i'm using:

singleline: 'value',
singleline: "value",
multiline: ```
  line 1
  line 2
```
查看更多
看风景的人
5楼-- · 2018-12-31 17:07

write any text in notepad++ with multi multi line then press ctl+a and ctl+j to arrange them in one line

In json file write :

{
"key":"multi line copied from notepad++"
}
查看更多
孤独总比滥情好
6楼-- · 2018-12-31 17:08

Is it possible to have multi-line strings in JSON?

Yes. I just tested this now with my Firefox web browser by pressing F12, clicking console and typing at the bottom of the screen.

x={text:"hello\nworld"}

Object x has just been created from a JSON format string containing a multi-line string.

console.log(x.text)
hello
world

x.text is displayed showing that it is a multi-line string.

These two tests show that Firefox's Javascript interpreter is happy to create and use JSON with multiline strings.

More tests with JSON.stringify and JSON.parse showed the Javascript interpreter can convert an object containing multiline strings to JSON and parse it back again with no problem at all.

I have in the past stored the complete works of Shakespeare as a property in a JSON object and then sent it over the internet, uncorrupted.

Example

Here is a two line string entered over three lines

x={text:"expert\
s\nex\
change"}

We can display the object

console.log(x)

giving

Object { text: "experts\nexchange" }

or the string

console.log(x.text)

giving

experts
exchange

The end of lines in the string result from using \n and the multiple input lines are achieved using just \ at the end of the line.

In practice you might want to synchronize your line endings with the ones in the string, e.g.

x={text:"experts\n\
exchange"}

Multi-Line String Length

console.log("Hello\nWorld".length)
11 
console.log("Hello World".length)
11

Note that the string with the newline is not longer than the string with the space. Even though two characters were typed on the keyboard ('\' and 'n'), only one character is stored in the string.

查看更多
皆成旧梦
7楼-- · 2018-12-31 17:09

Try using base64 encoded string value. Worked for me most of the time.

{
    "singleLine": "Some singleline String",
    "multiline": ["Line one", "line Two", "Line Three"]
} 

after base64 encoded string look like

{
    "singleLine": "Some singleline String",
    "multiline": "TGluZSBvbmUKTGluZSBUd28KTGluZSBUaHJlZQ=="
} 

base64 encoding and decoding is available in all languages.

查看更多
登录 后发表回答