2014-02-11 32 views
0

這是我的JSON文件訪問更深層次的對象與Mustache.JS

{ 
    "topic": { 
    "topicTitle": "Living things and the environment", 
    "subTopics": [ 
    { 
    "subTopicTitle": "Identifying, namimg and classifying", 
    "subTopicShortName": "identifyingNamingClassifying", 
    "subTopicDescription": "About one and a half million kinds of organisms have been discovered. How do we identify, name and classify them?", 
    "subTopicQuestions": [ 
     { 
     "question": "Use the key to identify the plants a to e in picture 1.", 
     "subTopicQuestionAssetLinks": [ 
      "href://file/filename/image1.jpeg" 
     ], 
     "subTopicQuestionAssetCaptions": [ 
      "Caption 1" 
     ] 
     }, 

的一部分,這是我的javascript

<script> 
    $.getJSON('data.json', function(data1) { 
     intro_template = "<h1> {{topic.topicTitle}} </h1> <h2> {{topic.subTopics}} </h2>"; 
     html = Mustache.to_html(intro_template, data1); 
        $('.intro_template_container').html(html); 
    });  
</script> 

H1的作品完美展示「活物和環境」

在H2標籤之間我想展示「標識,名稱和分類」的「subTopicTitle」

這怎麼可能?

回答

1

由於subTopics是一個數組,你將不得不使用鬍鬚部分:

<h1>{{topic.topicTitle}}</h1> 
{{#topic.subTopics}} 
    <h2>{{subTopicTitle}}</h2> 
{{/topic.subTopics}} 
相關問題