How to read complex JSON object from jQuery in Servlet request.getParameter

I create and submit a JSON object using jQuery, but I canโ€™t figure out how to parse it properly in my Ajax servlet using the org.json.simple library.

My jQuery code is as follows:

var JSONRooms = {"rooms":[]}; $('div#rooms span.group-item').each(function(index) { var $substr = $(this).text().split('('); var $name = $substr[0]; var $capacity = $substr[1].split(')')[0]; JSONRooms.rooms.push({"name":$name,"capacity":$capacity}); }); $.ajax({ type: "POST", url: "ParseSecondWizardAsync", data: JSONRooms, success: function() { alert("entered success function"); window.location = "ctt-wizard-3.jsp"; } }); 

In the servlet, when I use request.getParameterNames () and print it on my console, I get the parameter names rooms[0][key] , etc., but I just can't parse the JSON Array rooms. I tried to parse the object returned by request.getParameter("rooms") or .getParameterValues("rooms") , but both of them return a null value.

Is there something wrong with the way I format JSON data in jQuery or is there a way to parse JSON into a servlet that I am missing?

Ask for more code, although the servlet is still pretty empty, as I cannot figure out how to parse the data.

+1
source share
1 answer

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. 
+2
source

Source: https://habr.com/ru/post/1402881/


All Articles