如何以JSON格式创建族树结构

时间:2013-11-02 13:03:24

标签: javascript json

我正在尝试使用JSON格式,并且不确定如何使用它来构建族树。这就是我所拥有的(为了保持简单,我只列出父亲,他的孩子以及这些孩子是否自己有孩子。我没有列出配偶的名字。)

{
    "Name": "Jonathan Smith",
    "Children": [
        {
            "name": "Adam",
            "Children": [
                {
                    "name": "Suzy",
                    "children": ""
                },
                {
                    "name": "Clare",
                    "children": ""
                },
                {
                    "name": "Aaron",
                    "children": ""
                },
                {
                    "name": "Simon",
                    "children": ""
                }
            ]
        },
        {
            "name": "Timmy",
            "Children": ""
        },
        {
            "name": "Alison",
            "Children": [
                {
                    "name": "Natasha",
                    "children": ""
                },
                {
                    "name": "Zak",
                    "children": ""
                }
            ]
        }
    ]
}

虽然,它的验证很好,但我不确定是否有最好的方法(例如,我的方法是DRY和可扩展的)。

4 个答案:

答案 0 :(得分:4)

最简单的方法:

{
     "Jonathan Smith": {
        "Adam": {
            "Suzy": {},
            "Clare": {},
            "Aaron": {},
            "Simon": {}
        }, 
        "Timmy": {},
        "Alison": {
            "Natasha": {}, "Zak": {}
        }
     }
}

更强大的结构:

{
    "Smiths": {
        "Jonathan Smith": { "id": 0},
        "Adam Smith": { "id": 1, "father": 0 },
        "Suzy Smith": { "id": 4, "father": 1 },
        "Clare Smith": { "id": 5, "father": 1 },
        "Aaron Smith": { "id": 6, "father": 1 },
        "Simon Smith": { "id": 7, "father": 1 },
        "Timmy Smith": { "id": 2, "father": 0 },
        "Alison Smith": { "id":3, "father": 0 },
        "Natasha Smith": { "id": 8, "father": 3 },
        "Zak Smith": { "id": 9, "father": 3 }
    }
}

添加更多关系,母亲,丈夫和妻子。

{
    "Smiths": {
        "Jonathan Smith": { "id": 0, "wife": [10]},
        "Suzan Smith": { "id": 10, "born": "Suzan Jones", "husband": [0] },
        "Adam Smith": { "id": 1, "father": 0, "mother": 10 },
        "Suzy Smith": { "id": 4, "father": 1 },
        "Clare Smith": { "id": 5, "father": 1 },
        "Aaron Smith": { "id": 6, "father": 1 },
        "Simon Smith": { "id": 7, "father": 1 },
        "Timmy Smith": { "id": 2, "father": 0, "mother": 10  },
        "Alison Smith": { "id":3, "father": 0, "mother": 10  },
        "Natasha Smith": { "id": 8, "father": 3 },
        "Zak Smith": { "id": 9, "father": 3 }
    }
}

有时使用Javascript

使用JSON要容易得多
var familyTree = {}
familyTree["Dick Jones"] = { id: 1234, father: 213 }

这将允许您添加,删除,使用函数,能够检查错误,然后通过调用获取生成的JSON:

JSON.stringify(familyTree)

答案 1 :(得分:0)

你必须要注意,因为你添加了格式json数据覆盖。尝试使用一种结构,允许您以简单的方式响应您想要执行的查询。

答案 2 :(得分:0)

试试这个:

{'name': 'John'}, {'name': 'Jack', 'child_of': 'John'}, {'name': 'Charlie', 'child_of': 'Jack', 'grand_child_of': 'John'}

答案 3 :(得分:0)

在JSON中使用树可能很困难,但也许您可以使用级别的概念(本例中的代),这样您就可以了解叔叔,表兄弟等。

    [
   {
      "id":100,
      "name":"Jhon Smith",
      "generation":1,
      "children":[
         {
            "id":101,
            "name":"Michael Smith",
            "generation":2,
            "children":null
         },
         {
            "id":102,
            "name":"Diana Smith",
            "children":[
               {
                  "id":301,
                  "name":"Britney Smith",
                  "generation":3,
                  "children":null
               }
            ]
         }
      ]
   },
   {
      "id":200,
      "name":"Richard Smith",
      "generation":1,
      "children":[
         {
            "id":101,
            "name":"Michael Smith",
            "generation":2,
            "children":null
         },
         {
            "id":102,
            "name":"Diana Smith",
            "generation":2,
            "children":null
         }
      ]
   }
]
相关问题