0

In my servlet I am creating a list key-value pairs with the help of a Map

Map map=new HashMap();  
map.put("1", "john");  
map.put("2", "cris");  
map.put("3","patrik");  
JSONObject jsonMap=new JSONObject(map);  

out.print(jsonMap);  

I am calling the above servlet through ajax. I want to know how can I print all the key-value pairs in my javascript(with and without using jquery) without knowing the key values?

Any other idea how can I get both key-value pairs from servlet in javascript using ajax ?

Thanks

mukund
  • 2,866
  • 5
  • 31
  • 41

2 Answers2

0

Though not safe, you can use eval to convert this response to a JSON object on the javascript side as follows:

eval("var json = " + xmlHttpRequest.responseText);
for (var i in json) {
    // i and json[i] are the key and values respectively.
}
Community
  • 1
  • 1
Vikdor
  • 23,934
  • 10
  • 61
  • 84
0

you can use the javascript arrays if you don't know the key. i.e.

var resp = JSON.parse(responseText);
for(i=0; i<resp.length;i++){
    console.log(resp[i]);
}
Prog Mania
  • 615
  • 9
  • 14