例如:
[{
id:'our-purpose',
title:'Our Purpose',
slug:'/our-purpose',
backgroundImage:'images/bg-our-purpose.jpg',
showInNav:1
},
{
id:'our-people',
title:'Our People',
slug:'/our-people',
backgroundImage:'images/bg-our-people.jpg',
showInNav:1,
subpages:[
{
id:'attorneys',
title:'Attorneys',
slug:'/our-people/attorneys',
subpages:[
{
id:'attorneys-cdb',
title:'Attorneys - Carla DeLoach Bryant',
slug:'/our-people/attorneys/carla'
},
{
id:'attorneys-jad',
title:'Attorneys - Jordan A. DeLoach',
slug:'/our-people/attorneys/jordan'
},
{
id:'attorneys-shh',
title:'Attorneys - Sarah H. Hayford',
slug:'/our-people/attorneys/sarah'
},
{
id:'attorneys-jsp',
title:'Attorneys - Jason S. Palmisano',
slug:'/our-people/attorneys/jason'
},
{
id:'attorneys-ldw',
title:'Attorneys - Lindsey DeLoach Wagner',
slug:'/our-people/attorneys/carla'
},
]
},
{
id:'legal-support',
title:'Legal Support',
slug:'/our-people/legal-support',
subpages:[
{
id:'legal-support-tb',
title:'Legal Support - Theolyn Brock',
slug:'/our-people/attorneys/theolyn'
},
{
id:'legal-support-cd',
title:'Legal Support - Cheri DeFries',
slug:'/our-people/attorneys/cheri'
},
]
},
//...and so on
你会注意到你可以做到,json[1].subpages[0].subpages[0]
但我不知道它会有多深。这是我的一位设计师客户为他正在为客户构建的 AJAX 网站编写的。我正在尝试生成导航,并且需要能够:
A. 递归解析这个来构建一个导航(<ul><li><a>...
)
B. 搜索匹配的 id。像这样(但这不是递归的)[并忽略for...in
,这只是为了举例)
var matchId(id,json){
for(x in json){
if(json[x].id == id){ var theMatch = json[x]; break; }
}
}