Yaml语法创建此数组

4

给定以下简单的yaml数据,

foo: 1
bar:
  - one
  - two

如果我想创建一个与原始数据结构完全相同的数组,正确的方法是什么?
我已经尝试过:
first:
  foo: 1
  bar:
    - one
    - two
    - three
second:
  foo: 2
  bar:
    - one1
    - two2
    - three3

或者,
- foo: 1
  bar:
    - one
    - two
    - three
- foo: 2
  bar:
    - one1
    - two2
    - three3

并且,此外,
- first:
    foo: 1
    bar:
      - one
      - two
      - three
- second:
    foo: 2
    bar:
      - one1
      - two2
      - three3

但似乎没有正确的方式。有什么帮助吗?谢谢!

1个回答

5
我认为你需要的是这个:

我认为您需要的是以下内容:

- foo: 1
  bar:
    - one
    - two
    - three
- foo: 2
  bar:
    - one1
    - two2
    - three3

这将给您提供以下结构:

[
  {
    "foo": 1, 
    "bar": [
      "one", 
      "two", 
      "three"
    ]
  }, 
  {
    "foo": 2, 
    "bar": [
      "one1", 
      "two2", 
      "three3"
    ]
  }
]

如果“first”和“second”标签对您很重要,可以使用以下代码:

或者这样:

first:
  foo: 1
  bar:
    - one
    - two
    - three
second:
  foo: 2
  bar:
    - one1
    - two2
    - three3

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

{
  "second": {
    "foo": 2, 
    "bar": [
      "one1", 
      "two2", 
      "three3"
    ]
  }, 
  "first": {
    "foo": 1, 
    "bar": [
      "one", 
      "two", 
      "three"
    ]
  }
}

完美!你是如何解释它的?有没有任何在线工具可以帮助解释YAML?谢谢! - xpt
2
这是一个简单但非常有用的在线YAML解析工具:http://yaml-online-parser.appspot.com/ - Alex Taylor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接