Represent hierarchy in a JSON object
I need to represent this hierarchy in a JSON object. Can someone help me out ?
- John
--- Lee
------ Nash
--------- Tim
------ Nicole
------ Kelly
--- Alice
--- Stanley
开发者_如何学C
{
"name": "John",
"children": [
{
"name": "Lee",
"children": [
{
"name": "Nash",
"children": [{ "name":"Tim"}]
},
{
"name": "Nicole"
},
{
"name": "Kelly"
}
]
},
{
"name": "Alice"
},
{
"name": "Stanley"
}
]
}
How about this:
{
"John" : {
"Lee" : {
"Nash" : {
"Tim" : null
},
"Nicole" : null,
"Kelly" : null
},
"Alice" : null,
"Stanley" : null
}
}
The relationship, whether it be children or otherwise, is implied by the tree hierarchy.
["John", [
["Lee", [
["Nash", [
["Tim"]
]],
["Nicole"],
["Kelly"]
]],
["Alice"],
["Stanley"]
]]
Similar to the accepted answer but I think it's better to make it an array at the top level, otherwise you can only support one value at the root level. This way the whole data structure is also recursive.
[
{
"name": "John",
"children": [
{
"name": "Lee",
"children": [
{
"name": "Nash",
"children": [{ "name":"Tim"}]
},
{
"name": "Nicole"
},
{
"name": "Kelly"
}
]
},
{
"name": "Alice"
},
{
"name": "Stanley"
}
]
}
]
Try something like this:
{"name": "John", "children": [ {"name": "Lee", "children": {...}}, {name:"Alice", "children": {..}} ] }
精彩评论