【问题标题】:How to nest objects and arrays in a YAML file?如何在 YAML 文件中嵌套对象和数组?
【发布时间】:2018-09-04 18:17:17
【问题描述】:

我目前有这个 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。你用的是什么文本编辑器?

标签: javascript yaml markdown


【解决方案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"
    }
  ]
}

【讨论】:

    【解决方案2】:

    您的 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://stackoverflow.com/a/16335038/942648

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2020-07-18
      • 1970-01-01
      • 2021-12-17
      • 2018-05-20
      相关资源
      最近更新 更多