在Servlet内检索从JSON发送为JSON的数据 [英] Retrieve the data sent as JSON from JavaScript, inside a servlet
问题描述
我在从Java servlet内的JavaScript检索以JSON形式发送的数据时有一个查询. 以下是我在做什么...
I have a query on retrieving data sent as JSON from a JavaScript, inside a Java servlet. Following is what I am doing...
这是JavaScript内向Servlet发出请求的代码的一部分
This is the part of the code inside JavaScript making a request to a servlet
type : "POST",
url: 'getInitialData',
datatype: 'json',
data : ({items :[{ name: "John", time: "2pm" },{name: "Sam", time: "1pm" }]}),
success: function(data) {
try{
////Code to be handeled where response is recieved
}catch(e){
alert(e);
}
}
在发出此请求时,我尝试检索从Servlet中的JavaScript发送的参数,但这样做时,我首先对如何从请求中检索数据感到困惑
On making this request I try to retrieve the parameters sent from JavaScript in a Servlet, but while doing so I was firstly confused on how to retrieve the dat from the request
我在servlet中使用了以下内容:
I used the following in my servlet:
注意::我的Servlet中的内容类型设置为: apllication/json
NOTE : the content Type in my Servlet is set to : apllication/json
response.setContentType("application/json");
request.getParameterMap();
上面显示的数据如下,但我无法弄清楚如何工作并获取实际数据
the above showed me the data as below, but I was not able figure out how to work and get the actual data
{items[1][name]=[Ljava.lang.String;@1930089, items[0][time]=[Ljava.lang.String;@860ba, items[1][time]=[Ljava.lang.String;@664ca, items[0][name]=[Ljava.lang.String;@1c334de}
而以下代码为我提供了预期为null的异常.
while the following code gave me Exception of null which was expected.
request.getParametervalues("items");
在我尝试过的其他请求中,其中request.getParameter(); request.getParameterNames();但徒劳...
Among the others i tried where request.getParameter(); request.getParameterNames(); but in vain...
我的方向错误吗?请指导我! 请让我知道如何重新获得这些价值.
Am I in a wrong direction? Please guide me! Please let me know how to retieve these value.
感谢您阅读这篇冗长的文章...
Thank You for reading this long post...
Sangeet
推荐答案
请求参数映射是Map<String, String[]>
,其中映射键是参数名称,映射值是参数值--HTTP允许在一个以上的值相同的名字.
The request parameter map is a Map<String, String[]>
where the map key is the parameter name and map value are the parameter values --HTTP allows more than one value on the same name.
鉴于地图的打印输出,应该可以进行以下操作:
Given the printout of your map, the following should work:
String item0Name = request.getParameter("items[0][name]");
String item0Time = request.getParameter("items[0][time]");
String item1Name = request.getParameter("items[1][name]");
String item1Time = request.getParameter("items[1][time]");
如果您想获得更多动态效果,请使用以下命令:
If you want a bit more dynamics, use the following:
for (int i = 0; i < Integer.MAX_VALUE; i++) {
String itemName = request.getParameter("items[" + i + "][name]");
String itemTime = request.getParameter("items[" + i + "][time]");
if (itemName == null) {
break;
}
// Collect name and time in some bean and add to list yourself.
}
请注意,设置响应内容类型与收集请求参数无关.
Note that setting the response content type is irrelevant when it comes to gathering the request parameters.
这篇关于在Servlet内检索从JSON发送为JSON的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!