2014-03-14 48 views
0

JSON數據有一個好的一天程序員錯誤在處理內部的jQuery AJAX每個功能

說明: 我在我的代碼下拉菜單中,每當用戶選擇一些類別我需要搜索從JSON文件中值。我完成了這個通過改變事件它運作良好。

$('#social_media').on('change',function(e){ 
     var test = this.value; 
     $.ajax({ 
     url:"jsoncontent.json", 
     dataType:"json", 
     success:function(data){ 
      //here test variable contain 'facebook' value 
      //so i gave data.test instead of data.facebook 
      $.each(data.test,function(key,value){    
        alert(value.name); 
       });  
      } 
     }); 
    }); 

問題:這是我在每一個循環,我給data.test,而不是問題'data.facebook'因爲測試是包含價值的Facebook變量'我知道數據是對象和測試是變量,當然我確實在處理對象和變量時出錯。這是我的json數據

{ 
    "StackOverflow": [ 
     { 
      "name": "StackOverflow", 
      "exp": "3month", 
      "username": "koushik", 
      "Designation": "Software Developer" 
     }, 
     { 
      "name": "StackOverflow", 
      "exp": "3month", 
      "username": "koushik", 
      "Designation": "Software Developer" 
     }, 
     { 
      "name": "StackOverflow", 
      "exp": "3month", 
      "username": "koushik", 
      "Designation": "Software Developer" 
     }, 
     { 
      "name": "StackOverflow", 
      "exp": "3month", 
      "username": "koushik", 
      "Designation": "Software Developer" 
     } 
    ], 
    "facebook": [ 
     { 
      "name": "facebook", 
      "exp": "3month", 
      "username": "koushik", 
      "Designation": "Software Developer" 
     }, 
     { 
      "name": "facebook", 
      "exp": "3month", 
      "username": "koushik", 
      "Designation": "Software Developer" 
     }, 
     { 
      "name": "facebook", 
      "exp": "3month", 
      "username": "koushik", 
      "Designation": "Software Developer" 
     }, 
     { 
      "name": "facebook", 
      "exp": "3month", 
      "username": "koushik", 
      "Designation": "Software Developer" 
     } 
    ], 
    "twitter": [ 
     { 
      "name": "twitter", 
      "exp": "3month", 
      "username": "koushik", 
      "Designation": "Software Developer" 
     }, 
     { 
      "name": "twitter", 
      "exp": "3month", 
      "username": "koushik", 
      "Designation": "Software Developer" 
     }, 
     { 
      "name": "twitter", 
      "exp": "3month", 
      "username": "koushik", 
      "Designation": "Software Developer" 
     }, 
     { 
      "name": "twitter", 
      "exp": "3month", 
      "username": "koushik", 
      "Designation": "Software Developer" 
     } 
    ] 
} 

提前感謝

+0

只是看看http://stackoverflow.com/questions/10084234/how-to-parse-multi-dimensional-json-data-through-javascript 爲了您更好地瞭解如何訪問multidimensinal JSOn數據。 – Rahul

回答

0

試試這個:

$.each(data[test], function (key, value) { 
    alert(value.name); 
}); 
} 

如果test是一個變量,然後我們用data[test]data.test

0

您需要創建另一個$.each第一$.each 所以第二$.each內應採取「價值」的價值

$.each(obj, (key, value) { 
      $.each(obj, (keys, values) {} 
      } 

這是類似的東西,就我認爲。