2014-03-14 43 views
0

這是我的Ajax代碼錯誤在訪問JSON數據

$.ajax({ 
     url:"jsoncontent.json", 
     dataType:"json", 
     success:function(data){ 
      var tem = data; 
      var test ='facebook'; 
      alert(tem.facebook[0].name);//working 
      alert(tem.test[0].name);//Why it is not Working?How can i access with test variable 
        //alert(tem.test+[2].name);tried  
      } 
     }); 

我曾在訪問JSON數據混淆..任何幫助

+1

你試過tem [test] [0] .name? – anand4tech

+0

生成的JSON是什麼樣的? –

+0

向我們展示您的json –

回答

2

您需要使用Bracket notation

因此使用

alert(tem[test][0].name); 

代替

alert(tem.test[0].name); 

編輯:根據意見。你應該訪問official jQuery.each() docs

$.each(tem[test],function(index, value){ 
    alert(value.name); 
}); 
+0

Ya我得到了答案..我還有一個疑問先生?我如何通過jQuery訪問每個函數先生? – kamesh

+1

@Koushik,你可以詳細說明'我怎樣才能通過jQuery訪問每個函數的先生' – Satpal

+0

我想單獨訪問這個tem [test] [0] .name,tem [test] [1] .name ,...使用jquery $ .each(whatvalue?,function(k,v){alert(how?);}); – kamesh