2013-11-05 23 views
0

也許我有一個類似的問題,但我無法得到正確的關鍵字,所以請耐心等待。Ajax響應中的json對象的訪問列表

我有這樣的數據在可以將Java類

List<String> listOne = some data list from the database 
List<String> listTwo = another data list from the database 

我要第一個給這個兩List爲JSON對象到客戶端,以便我的想法是這樣的:

Map<String, List<String>> map = new HashMap<String, List<String>>(); 
map.put(listOne); 
map.put(listTwo); 

return new Gson().toJson(map); 

List<List<String>> list = new ArrayList<List<String>>(); 
list.add(listOne); 
list.add(listTwo); 

return new Gjson().toJson(map); 

我不知道哪個更好。 我想要做的下一件事就是訪問給客戶端

$.post('some url', someData : 'that requires to know which data will be fetched', 
     function(data) { 
     // here how can I access the two list inside the list 
     // or the list inside the map 
     // i want to have a full control to the data 
     // example: 
     // Iterate the listOne on this $('#someId').append(listOne); 
     // Iterate the listTwo on this $('#anotherId').append(listTwo); 

     }, json); 

什麼是正確做到這一點的呢?如果我的問題不清楚,請發表評論,我會作出相應迴應。在此先感謝

回答

0

首先,你應該解決您的代碼:

Map<String, List<String>> map = new HashMap<String, List<String>>(); 
map.put("listOne", listOne); 
map.put("listTwo", listTwo); 

return new Gson().toJson(map); 

其次,你應該使用$.GetJSON()功能:

$.getJSON('some url', function(data) { 
    //Do something with data 

}); 

然後,您可以用data.listOnedata.listTwo直接訪問數據。