2012-07-20 60 views
0

可能重复:
How to access this json object from handlebarsjs如何访问一个嵌套的JSON对象

我有此JSON文件:

[{   
    "id" : 9, 
    "name" : "Name1", 
    "address" : "address1", 
    "city" : "city1", 
    "state" : "KS", 
    "zip" : "11111", 
    "country" : "USA", 
    "fax" : "111111", 
    "phone" : "1111111", 
    "website" : "", 
    "account" : "11111", 
    "contacts" : [] 
}, 
{ 
    "id" : 12, 
    "name" : "Name2", 
    "address" : "address2", 
    "city" : "city2", 
    "state" : "NJ", 
    "zip" : "11111", 
    "country" : "USA", 
    "fax" : "", 
    "phone" : "1111", 
    "website" : "", 
    "account" : "11111", 
    "contacts" : [ { 
     "firstName" : "name", 
     "lastName" : "lastname", 
     "title" : "rep", 
     "phone" : "3333", 
     "email" : "33333" 
    } ] 
} ] 

我可以访问这些属性

<td>{{:name}}</td> 
<td>{{:phone}}</td> 
<td>{{:city}}</td> 

但是,如何从模板访问contacts.firstName

+1

我只是在这里抽出我的水晶球!突然之间,我看到你的整个模板系统,答案是'​​{{:contacts.firstName}}'?????更严重的是,谁知道,这取决于所使用的模板系统? – adeneo 2012-07-20 21:46:54

+0

尝试它没有工作,我coudnt看到从JsRender关于这个 – skystar7 2012-07-20 21:49:14

+0

好的例子,它真的是一个数组,所以也许'​​{{:contacts [0] .firstName}}' – adeneo 2012-07-20 21:56:13

回答

0

您的联系人是一个数组。因此,如果你想要第一个,请执行以下操作:

{{:contacts[0].firstName}} 

否则,您需要使用for标记的循环。