0

How can i access the HashMap key to the JQuery? I want to have the key it self to use in typeahead of bootstrap.

I'm using Gson to parse the HashMap to 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 ) );

Code of my typeahead:

$('#textbox').typeahead({
    source: function(query, process){
        return $.get('URL', {query: query},
                      function(data){
              return process(data.key);
              },'json');
        },
    items: 10
});

at return process(data); i want to have the key of the HashMap it self

newbie
  • 1,884
  • 7
  • 34
  • 55
  • You probably want `process(data)` since the map **is** the root JSON element. Note, though, that [you can't return anything from an ajax call since it's asynchronous.](http://stackoverflow.com/q/14220321/139010) – Matt Ball Sep 25 '13 at 15:43
  • So i can only return 1 array of value? i tried data.key because i want it to return the key not the value. Isn't that possible? – newbie Sep 25 '13 at 15:51
  • No - you can't **return** anything from an ajax call. I don't understand what you mean by "the key". Try looking at the output of [`Object.keys(data)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys). – Matt Ball Sep 25 '13 at 16:30
  • I see, i'll search for a work around – newbie Sep 26 '13 at 13:41

1 Answers1

0

Why its highly possible.

just use JSON.stringfy(data.key);