YAML - 序列样式



要理解序列样式,理解集合非常重要。集合和序列样式的概念并行运作。YAML 中的集合用适当的序列样式表示。如果您想要引用适当标记的顺序,请始终参考集合。YAML 中的集合按从 0 开始的顺序整数编制索引,如数组中表示的那样。序列样式的重点从集合开始。

示例

我们把宇宙中的行星数量视为一个集合中创建的序列。以下代码显示了如何表示宇宙中行星的序列样式 −

# 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"
   ]
}
广告