如何使用ajax从java脚本中的servlet获取键值对?

时间:2023-01-20 07:38:11

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

在我的servlet中,我在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?

我通过ajax调用上面的servlet。我想知道如何在不知道键值的情况下打印我的javascript中的所有键值对(使用和不使用jquery)?

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

任何其他想法如何使用ajax从javascript中获取servlet中的两个键值对?

Thanks

2 个解决方案

#1


0  

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

虽然不安全,但您可以使用eval将此响应转换为javascript端的JSON对象,如下所示:

eval("var json = " + xmlHttpRequest.responseText);
for (var i in json) {
    // i and json[i] are the key and values respectively.
}

#2


0  

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

如果你不知道密钥,你可以使用javascript数组。即

var resp = JSON.parse(responseText);
for(i=0; i<resp.length;i++){
    console.log(resp[i]);
}

#1


0  

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

虽然不安全,但您可以使用eval将此响应转换为javascript端的JSON对象,如下所示:

eval("var json = " + xmlHttpRequest.responseText);
for (var i in json) {
    // i and json[i] are the key and values respectively.
}

#2


0  

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

如果你不知道密钥,你可以使用javascript数组。即

var resp = JSON.parse(responseText);
for(i=0; i<resp.length;i++){
    console.log(resp[i]);
}