繁体   English   中英

在 JSON 对象中表示层次结构

[英]Represent hierarchy in a JSON object

我需要在 JSON 对象中表示这个层次结构。 有人可以帮我吗 ?

- John
--- Lee
------ Nash
--------- Tim
------ Nicole
------ Kelly
--- Alice
--- Stanley
{
  "name": "John", 
  "children": [ 
    {
      "name": "Lee", 
      "children": [
         {
           "name": "Nash", 
           "children": [{ "name":"Tim"}]
         },
         {
           "name": "Nicole"
         },
         {
           "name": "Kelly"
         }
      ]
    },
    {
      "name": "Alice"
    },
    {
      "name": "Stanley" 
    } 
  ] 
}

这个怎么样:

{
    "John" : {
        "Lee" : {
            "Nash" : {
                "Tim" : null 
            },
            "Nicole" : null,
            "Kelly" : null 
        },
        "Alice" : null,
        "Stanley" : null 
    }
}

这种关系,无论是孩子还是其他,都由树层次结构暗示。

["John", [
    ["Lee", [
        ["Nash", [
            ["Tim"]
        ]],
        ["Nicole"],
        ["Kelly"]
    ]],
    ["Alice"],
    ["Stanley"]
]]

与接受的答案类似,但我认为最好将其设为顶层数组,否则您只能在根级别支持一个值。 这样整个数据结构也是递归的。

[
  {
    "name": "John", 
    "children": [ 
      {
        "name": "Lee", 
        "children": [
          {
            "name": "Nash", 
            "children": [{ "name":"Tim"}]
          },
          {
            "name": "Nicole"
          },
          {
            "name": "Kelly"
          }
        ]
      },
      {
        "name": "Alice"
      },
      {
        "name": "Stanley" 
      } 
    ]
  }
]

尝试这样的事情:

{"name": "John", "children": [ {"name": "Lee", "children": {...}}, {name:"Alice", "children": {..}} ] }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM