0
Json Объект:Traverse объектов и построить дерево Lineage
var json = {
"Tree": [{
"Title": "Condition",
"Attr": {
"Id": 2258,
"Zone": null
},
"Children": [{
"Title": "General Wellness",
"Attr": {
"Id": 2315,
"Zone": null
},
"Children": [{
"Title": "Family Health",
"Attr": {
"Id": 2262,
"Zone": null
},
"Children": []
}, {
"Title": "Healthy Home",
"Attr": {
"Id": 2316,
"Zone": null
},
"Children": []
}, {
"Title": "Vitamins",
"Attr": {
"Id": 2317,
"Zone": null
},
"Children": []
}, {
"Title": "Recipes",
"Attr": {
"Id": 2318,
"Zone": null
},
"Children": []
}, {
"Title": "Caregiving",
"Attr": {
"Id": 2325,
"Zone": null
},
"Children": []
}, {
"Title": "Healthy Eating",
"Attr": {
"Id": 2346,
"Zone": null
},
"Children": []
}, {
"Title": "Travel Health",
"Attr": {
"Id": 2347,
"Zone": null
},
"Children": []
}]
}]
}]
}
Я могу пройти через дерево, однако я не могу построить древовидную структуру, например, если я хочу, чтобы искать «Рецепты» он должен вернуть мне результат как:
Состояние> Общие> Рецепты Wellness
UPDATE:
Перемещение осуществляется с помощью:
function process(key,value) {
alert(key + " : "+value);
}
function traverse(o,func) {
for (var i in o) {
func.apply(this,[i,o[i]]);
if (o[i] !== null && typeof(o[i])=="object") {
traverse(o[i],func);
}
}
}
traverse(json,process);
JSON недействителен http://jsonlint.com/ проверить здесь. – Rhea
@ depperm добавлена логика обхода –
@ Rhea исправил Json –