Nested List from JSON
HTML
<div id="list"></div>
JavaScript
var allData = [
{
"Identification": "AB461881",
"SupportedLanguages": [
"en",
"fr",
"es",
"de"
],
"ATMServices": [
"CashWithdrawal",
"PINChange",
"MobilePhoneTopUp",
"Balance",
"MiniStatement",
"CharityDonation"
],
"SupportedCurrencies": [
"GBP"
],
"MinimumPossibleAmount": "5",
"Branch": {
"Identification": "402529"
},
"Location": {
"Site": {
"Name": "ILKESTON I1"
},
"PostalAddress": {
"StreetName": "63 Bath Street",
"TownName": "ILKESTON",
"Country": "GB",
"PostCode": "DE7 8DD",
"GeoLocation": {
"GeographicCoordinates": {
"Latitude": "52.9740105",
"Longitude": "-1.3090999"
}
}
}
}
}
];
var array = ["<ul>"];
function printList(items) {
switch ($.type(items)) {
case "object":
getChildren(items);
break;
case "string":
array.push("<li>" + items + "</li>");
//console.log(items);
break;
case "array":
printArray(items);
break;
}
}
function getChildren(parent) {
for (var child in parent) {
//console.log(child);
array.push("<li>" + child + "<ul>");
printList(parent[child]);
array.push("</ul></li>");
}
}
function printArray(myArray){
for(var i = 0; i < myArray.length; i++){
//console.log(myArray[i]);
array.push("<li>" + myArray[i] + "</li>");
}
}
printList(allData);
array.push("<ul>");
$("#list").html(array.join(""));
//printList("string");
console.log(array);