如何访问嵌套的json对象

时间:2012-07-20 21:44:26

标签: jquery jquery-plugins jsrender

  

可能重复:
  How to access this json object from handlebarsjs

我有这个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 个答案:

答案 0 :(得分:0)

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

{{:contacts[0].firstName}}

否则你需要使用for标签循环。