YAML 序列样式
要理解序列样式,重要的是要理解集合。集合的概念和序列样式是并行的。在YAML中,集合用适当的序列样式表示。如果你想引用标签的适当顺序,总是要参考集合。在YAML中,集合是用按顺序从零开始的整数索引表示的,就像在数组中一样。序列样式的重点从集合开始。
示例
让我们把宇宙中的行星数量作为一个可以创建的序列,它可以被表示为一个集合。以下代码展示了如何表示宇宙中行星的序列样式:
# Ordered sequence of nodes in YAML STRUCTURE
Block style: !!seq
- Mercury # Rotates - no light/dark sides.
- Venus # Deadliest. Aptly named.
- Earth # Mostly dirt.
- Mars # Seems empty.
- Jupiter # The king.
- Saturn # Pretty.
- Uranus # Where the sun hardly shines.
- Neptune # Boring. No rings.
- Pluto # You call this a planet?
Flow style: !!seq [ Mercury, Venus, Earth, Mars, # Rocks
Jupiter, Saturn, Uranus, Neptune, # Gas
Pluto ] # Overrated
然后,您可以在JSON格式中看到有序序列的以下输出−
{
"Flow style": [
"Mercury",
"Venus",
"Earth",
"Mars",
"Jupiter",
"Saturn",
"Uranus",
"Neptune",
"Pluto"
],
"Block style": [
"Mercury",
"Venus",
"Earth",
"Mars",
"Jupiter",
"Saturn",
"Uranus",
"Neptune",
"Pluto"
]
}