如何在YAML文件中嵌套对象和数组?

5

我目前有一个JS数组,想将其转换为YAML文件。我尝试查找了一些资料,知道如何创建对象和数组,但不知道如何嵌套它们。

cartParams: [
    {
        title: Leg type,
        options: [
            Round, 
            Square
        ]
    },
    {
        title: Leg width / diameter,
        options: [
            1.00 inches,
            1.25 inches,
            1.50 inches,
            1.75 inches,
            2.00 inches
        ]
    }
]

仅提醒一下,有些文本编辑器可以让你预览YAML文件。你使用的是哪些文本编辑器? - JaeGeeTee
2个回答

10

您的YAML文件可能看起来像这样:

cartParams:
  - title: Leg type 
    options:
      - Round
      - Square
  - title: Leg width / diameter 
    options:
      - 1.00 inches
      - 1.25 inches
      - 1.50 inches
      - 1.75 inches
      - 2.00 inches

顺便提一下,YAML是JSON的超集,这意味着每个JSON对象也是有效的YAML对象。因此,您也可以使用对象的JSON表示。

有关使用特定于YAML的语法嵌套列表的更多信息,请参阅此处:https://dev59.com/6GQo5IYBdhLWcg3wE8C2#16335038


1
cartParams:
  - title: Leg type 
    options: [
     Round,
     Square
    ]
  - title: Leg width / diameter 
    options: [
     1.00 inches,
     1.25 inches,
     1.50 inches,
     1.75 inches,
     2.00 inches
    ]

应该输出以下内容:
{
  "cartParams": [
    {
      "options": [
        "Round", 
        "Square"
      ], 
      "title": "Leg type"
    }, 
    {
      "options": [
        "1.00 inches", 
        "1.25 inches", 
        "1.50 inches", 
        "1.75 inches", 
        "2.00 inches"
      ], 
      "title": "Leg width / diameter"
    }
  ]
}

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