The data $.ajax() argument accepts a JS object that represents the map of the request parameters. This way, any JS object that you pass it will be converted to query parameters. Since you pass simple vanilla to the JS object, it is treated as a map of request parameters. You need to access individual parameters using the name of the request parameter name.
String name1 = request.getParameter("rooms[0][name]"); String capacity1 = request.getParameter("rooms[0][capacity]"); String name2 = request.getParameter("rooms[1][name]"); String capacity2 = request.getParameter("rooms[1][capacity]"); // ...
You can find them all in the HttpServletRequest#getParameterMap() method:
Map<String, String[]> params = request.getParameterMap();
You can even dynamically collect all parameters as follows:
for (int i = 0; i < Integer.MAX_VALUE; i++) { String name = request.getParameter("rooms[" + i + "][name]"); if (name == null) break; String capacity = request.getParameter("rooms[" + i + "][capacity]");
If you intend to pass it as a real JSON object so that you can use the JSON parser to break it further into properties, then you need to convert it to String before sending using JS / jQuery and specify the data argument as follows:
data: { "rooms": roomsAsString }
Thus, it is available as a JSON string using request.getParameter("rooms") , which you can parse differently using an arbitrary JSON API.
Not tied to a specific problem, do not use the $ variable prefix in jQuery for objects other than jQuery. This makes your code more confusing for JS / jQuery experts. Use it only for real jQuery objects, not simple vanilla strings or primitives.
var $foo = "foo"; // Don't do that. Use var foo instead. var $foo = $("someselector"); // Okay.
source share