我正在尝试使用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": ""
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
虽然,它的验证很好,但我不确定是否有最好的方法(例如,我的方法是DRY和可扩展的).
最简单的方法:
{
"Jonathan Smith": {
"Adam": {
"Suzy": {},
"Clare": {},
"Aaron": {},
"Simon": {}
},
"Timmy": {},
"Alison": {
"Natasha": {}, "Zak": {}
}
}
}
Run Code Online (Sandbox Code Playgroud)
更强大的结构:
{
"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 }
}
}
Run Code Online (Sandbox Code Playgroud)
添加更多的关系,母亲,丈夫和妻子。
{
"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 }
}
}
Run Code Online (Sandbox Code Playgroud)
有时使用Javascript处理JSON要容易得多
var familyTree = {}
familyTree["Dick Jones"] = { id: 1234, father: 213 }
Run Code Online (Sandbox Code Playgroud)
这将允许您添加,删除,使用函数,能够检查错误,然后通过调用以下命令获取结果JSON:
JSON.stringify(familyTree)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7892 次 |
| 最近记录: |