在 TOML 文件上创建新行

Create new line on TOML file

我正在尝试了解 TOML 结构并且

[[servers]]
ip = "10.0.0.1"
role = "frontend"
[[servers]]
ip = "10.0.0.2"
role = "backend"

developer = "developer_name"

如果我解析上面的得到json作为

{
  "servers": [
    {
      "ip": "10.0.0.1",
      "role": "frontend"
    },
    {
      "developer": "developer_name",
      "ip": "10.0.0.2",
      "role": "backend"
    }
  ]
}

如您所见,开发人员嵌套在第二个对象中。但我需要根开发人员。

我用这个网站来验证TOMLTOML Parser

预期结果

{
      "servers": [
        {
          "ip": "10.0.0.1",
          "role": "frontend"
        },
        {
          
          "ip": "10.0.0.2",
          "role": "backend"
        }
      ],
      "developer": "developer_name"
    }
不保证 Toml table 中的

Key/value 对按任何特定顺序排列。 在根table中获取'developer'的方法是将其放在tables的'servers'数组之前:

developer = "developer_name"
[[servers]]
ip = "10.0.0.1"
role = "frontend"
[[servers]]
ip = "10.0.0.2"
role = "backend"

这将导致这个 Json 结构:

{
    "developer": "developer_name",
    "servers": [
    {
        "ip": "10.0.0.1",
        "role": "frontend"
    },
    {
        "ip": "10.0.0.2",
        "role": "backend"
    }]
}

这个小例子也可以用内联数组格式化:

servers = [{ip = "10.0.0.1", role = "frontend"}, {ip = "10.0.0.2", role = "backend"}]
developer = "developer_name"

这将导致相同的 Json 结构。