0
我怎樣才能訪問HashMap
密鑰到JQuery
?我想要在bootstrap的typeahead
中使用鑰匙。Java HashMap key to jQuery
我使用GSON解析HashMap來GSON:
我的預輸入的HashMap<String, String> source = new HashMap<String, String>();
put("key","value");
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(new Gson().toJson(source));
代碼:
$('#textbox').typeahead({
source: function(query, process){
return $.get('URL', {query: query},
function(data){
return process(data.key);
},'json');
},
items: 10
});
在return process(data);
我想有HashMap中它自身的關鍵
由於map **是根JSON元素,您可能需要'process(data)'。但請注意,[你不能從ajax調用中返回任何東西,因爲它是異步的。](http://stackoverflow.com/q/14220321/139010) –
所以我只能返回1個值的數組?我嘗試data.key,因爲我想它返回的關鍵不是價值。這不可能嗎? – newbie
不 - 你不能**從ajax調用返回**任何東西。我不明白你的意思是「鑰匙」。嘗試查看['Object.keys(data)']的輸出(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys)。 –