8

我正在尝试使用 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

4 回答 4

9

最简单的方法:

{
     "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)
于 2013-11-02T13:24:04.547 回答
0

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

于 2013-11-02T13:12:42.000 回答
0

试试这个:

{'name': 'John'}, {'name': 'Jack', 'child_of': 'John'}, {'name': 'Charlie', 'child_of': 'Jack', 'grand_child_of': 'John'}
于 2013-11-02T13:24:34.017 回答
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
         }
      ]
   }
]
于 2013-11-02T13:31:12.440 回答