2011-12-15 76 views
2

我正在構建Dojo移動應用程序。我有這樣一個JSON文件:用dojo顯示Json數據

{ 
    Introduction: 
    [ 
     { 
      title: "Introduction", 
      toolbar: "Page 1", 
      content: "cont, aabitant morbi tristique..." 
     }, 
     { 
      title: "Introduction", 
      toolbar: "Page 2", 
      content: "contesent vel nisi ipsum..." 
     } 
    ], 
    Services: 
    [ 
     { 
      title: "services", 
      toolbar: "Page 1", 
      content: "Cras adipiscing sapien nec..." 
     } 
    ] 
} 

下面的代碼打印的介紹寫在標題

dojo.xhrPost({ 
     url: "diet.json", 
     handleAs: "json", 
     load: function(response) { 
      console.log(response.Introduction[0].title); 
     } 
    }); 

我能夠得到內的數據。我怎樣才能得到第一標題即

  • 簡介
  • 服務

回答

3

所以你要在每個對象中的第一個冠軍有何反應?

for (key in response) 
    console.log(key + ": " + response[key][0].title); 

當然,這裏假定響應中每個數組中至少有一個元素。如果有些可能是空的,你會想要這樣的事情:

for (key in response) 
    console.log(key + ": " + 
       (response[key].length > 0 ? response[key][0].title : "empty")); 
+0

假設我不知道服務。我如何獲得服務? – 2011-12-15 16:32:03