I’m trying to get a JSON string (see below) to parse into a nested list (with no limit to the nesting level) by means of jQuery (since I use $.getJSON to get the string). currently I have a working but very un-jQuery method via a recursive function. from what I found in the web, mostly fixed dephth lists were used so these methods didn’t apply.
anyone an idea how to do that more jQuery-like?
// JSON
[
{
"title": "Home",
"href" : "example.com/home",
"children": [
{
"title": "about me",
"href" : "example.com/me",
"children": [
// I think you get the point …
]
}
]
},
{
"title": "sitemap",
"href" : "example.com/sitemap",
}
]
// current recursive function
function createItem(obj)
{
var html = '<li><a href="' + obj.href + '">' + obj.title + "</a>";
if (obj.children && obj.children.length) {
html += '<ul>';
for (var i = 0, l = obj.children.length; i < l; i++) {
html += createItem(obj.children[i]);
}
html += '</ul>';
}
return html + '</li>';
}
This post has been edited by Dormilich: 09 November 2011 - 06:41 AM

New Topic/Question
Reply


MultiQuote




|