创建此数组的 Yaml 语法

xpt*_*xpt 4 syntax yaml

鉴于以下简单的 yaml 数据,

foo: 1
bar:
  - one
  - two
Run Code Online (Sandbox Code Playgroud)

如果我想创建一个完全相同数据结构的数组,正确的方法是什么?

我试过了

first:
  foo: 1
  bar:
    - one
    - two
    - three
second:
  foo: 2
  bar:
    - one1
    - two2
    - three3
Run Code Online (Sandbox Code Playgroud)

或者,

- foo: 1
  bar:
    - one
    - two
    - three
- foo: 2
  bar:
    - one1
    - two2
    - three3
Run Code Online (Sandbox Code Playgroud)

并且,

- first:
    foo: 1
    bar:
      - one
      - two
      - three
- second:
    foo: 2
    bar:
      - one1
      - two2
      - three3
Run Code Online (Sandbox Code Playgroud)

但似乎没有一个是正确的方法。有什么帮助吗?谢谢!

Ale*_*lor 5

我想你是在追求这个:

- foo: 1
  bar:
    - one
    - two
    - three
- foo: 2
  bar:
    - one1
    - two2
    - three3
Run Code Online (Sandbox Code Playgroud)

这给了你这个结构:

[
  {
    "foo": 1, 
    "bar": [
      "one", 
      "two", 
      "three"
    ]
  }, 
  {
    "foo": 2, 
    "bar": [
      "one1", 
      "two2", 
      "three3"
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

或者,如果“第一”和“第二”标签对您很重要:

first:
  foo: 1
  bar:
    - one
    - two
    - three
second:
  foo: 2
  bar:
    - one1
    - two2
    - three3
Run Code Online (Sandbox Code Playgroud)

这为您提供了一个字典/关联数组:

{
  "second": {
    "foo": 2, 
    "bar": [
      "one1", 
      "two2", 
      "three3"
    ]
  }, 
  "first": {
    "foo": 1, 
    "bar": [
      "one", 
      "two", 
      "three"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个简单但非常有用的在线玩 YAML 的工具:http://yaml-online-parser.appspot.com/ (2认同)