Reputation: 10790
I need to represent this hierarchy in a JSON object. Can someone help me out ?
- John
--- Lee
------ Nash
--------- Tim
------ Nicole
------ Kelly
--- Alice
--- Stanley
Upvotes: 2
Views: 18784
Reputation: 3502
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"
}
]
}
]
Upvotes: 0
Reputation: 74
Try something like this:
{"name": "John", "children": [ {"name": "Lee", "children": {...}}, {name:"Alice", "children": {..}} ] }
Upvotes: -1
Reputation: 11557
{
"name": "John",
"children": [
{
"name": "Lee",
"children": [
{
"name": "Nash",
"children": [{ "name":"Tim"}]
},
{
"name": "Nicole"
},
{
"name": "Kelly"
}
]
},
{
"name": "Alice"
},
{
"name": "Stanley"
}
]
}
Upvotes: 9
Reputation: 4903
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.
Upvotes: 5
Reputation: 97601
["John", [
["Lee", [
["Nash", [
["Tim"]
]],
["Nicole"],
["Kelly"]
]],
["Alice"],
["Stanley"]
]]
Upvotes: 2