You can create a recursive loop function, but you have a problem: when the property is an object, the text is not displayed because there is no line. So you get:
- - value11
- value12
- value2
- value3
because while value2is the string displayed for item # 2, it is the object that is displayed for item # 1.
, , : http://jsfiddle.net/uXww2/.
function loop(obj, ul) {
$.each(obj, function(key, val) {
if(val && typeof val === "object") {
var ul2 = $("<ul>").appendTo(
$("<li>").appendTo(ul)
);
loop(val, ul2);
} else {
$("<li>", {
id: key
}).text(val).appendTo(ul);
}
});
}
$.getJSON('test.json', function(data) {
var ul = $("<ul>");
loop(data, ul);
ul.addClass("my-new-list").appendTo('body');
});