多行字符串中的 Yaml 文件转义字符

Yaml File escape characters in multiline string

我想在多行字符串中使用 \n 字符,但 YAML 不允许我使用 \n 字符。如何使用 \n 字符。

我的 YAML:

treeroot:
    branch1:
        name: >
            hello my friend\n how are you ?
            i am fine and you ?\n
            yes\nthanks
        branch1-1:
            name: Node 1-1
    branch2:
        name: Node 2
        branch2-1:
            name: Node 2-1

我知道,我们可以在单行模式下使用 \n 字符,但我想在多行模式下使用它。

YAML 中没有 'single-line' 模式。您有表示字符串的标量,它们可以是普通的(没有引号)、单引号和双引号。除此之外,在块样式中,您有文字和折叠样式。

文字和折叠样式始终是多行的(假设您用 |> 计算行数),但其他标量也可以是多行的。在后一种情况下,字符串中的双换行符转换为换行符,其他换行符转换为空格。由于只允许在双引号字符串中使用反斜杠转义,因此您需要使用以下 YAML:

treeroot:
    branch1:
        name: "hello my friend\n how are you ?
            i am fine and you ?\n
            yes\nthanks"
        branch1-1:
            name: Node 1-1
    branch2:
        name: Node 2
        branch2-1:
            name: Node 2-1

第一个键 'name' 的值与指定为

的值相同
"hello my friend\n how are you ? i am fine and you ?\n yes\nthanks"