Retrieve the data sent as JSON from JavaScript, inside a servlet

前端 未结 1 1216
我寻月下人不归
我寻月下人不归 2020-12-16 06:54

I have a query on retrieving data sent as JSON from a JavaScript, inside a Java servlet. Following is what I am doing...

This is the part of the code inside JavaScr

相关标签:
1条回答
  • 2020-12-16 07:20

    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.

    0 讨论(0)
提交回复
热议问题