문제

IMPORTANT: I am not asking about rendering strings as multiline.

I am talking about splitting a long string in a JSON into multiple lines in my source code when this string should logically be on a single line.

In short: I want source line breaking rules similar to HTML.

{
    "id": 550,
    "text": "this is long text "
            "very-very-very long text "
            "longer than you can imagine."
}

This text should be rendered as:

this is long text very-very-very long text longer than you can imagine.

The JSON is being referenced in JavaScript.

This is not a duplicate of Multiline strings in JSON because this question strongly refers to JavaScript and that question has no clear accepted answer.

도움이 되었습니까?

해결책

You can use multiple lines string representation in JavaScript:

JSON.parse('{"a" : "a\
asd"}')

Tried in console. It works.

다른 팁

According to JSONLint.com, the following multiline JSON is valid. So in short, yes, you can hit enter and break it into different lines.

{
    "a": 10,
    "b": 12,
    "c": 19
}

EDIT: I think I misread your question. I don't think you're able to break between a string like below. That does not work.

{
    "a": "abcde
    fg",
    "b": 12,
    "c": 19
}

Use an array:

{
    "id": 550,
    "text": ["this is long text ",
            "very-very-very long text ",
            "longer than you can imagine."]
}

Assuming that this goes in this.messages, use in code

this.messages.text.join();

You can't do it, you need to write the value in one line, if you want write in the next row, you need to put \n in your code

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top