Considering JSON tree structure listed below (it can be n level deep), I want to create a nested unordered list
var data = [{
'text': 'Subjects',
'data': [{
'text': 'Geography',
'data': []
}, {
'text': 'History',
'data': [{
'text': 'Ancient History',
'data': []
}]
}]
}, {
'text': 'Sports',
'data': []
}, {
'text': 'Music',
'data': []
}];
data can be nested 'n' level deep. You can have 'data' within 'data' which is within another 'data' and so on.
The output should be something like this
<ul>
<li>Subjects
<ul>
<li>Geography</li>
<li>History
<ul>
<li>Ancient History
</li>
</ul>
</li>
</ul>
</li>
<li>Sports
</li>
<li>Music
</li>
function json_tree(object) {
var json = "<ul>";
for (prop in object) {
var value = object[prop];
switch (typeof(value)) {
case "object":
json += "<li>" + value.text + json_tree(value) + "</li>";
break;
default:
json += "<li>" + value.text + "</li>";
}
}
return json + "</ul>";
}
I have tried using the above code, but this does not yield required results.