Question

I am creating and sending a JSON Object with jQuery, but I cannot 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 out to my console I get as parameter names rooms[0][key] etcetera, but I cannot parse the JSON Array rooms in any way. I have tried parsing the object returned by request.getParameter("rooms") or the .getParameterValues("rooms") variant, but they both return a null value.

Is there something wrong with the way I'm formatting the JSON data in jQuery or is there a way to parse the JSON in the servlet that I'm missing?

Ask for more code, even though the servlet is still pretty much empty since I cannot figure out how to parse the data.

Was it helpful?

Solution

The data argument of $.ajax() takes a JS object representing the request parameter map. So any JS object which you feed to it will be converted to request parameters. Since you're passing the JS object plain vanilla to it, it's treated as a request parameter map. You need to access the individual parameters by exactly their request parameter name representation instead.

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 by HttpServletRequest#getParameterMap() method:

Map<String, String[]> params = request.getParameterMap();
// ...

You can even dynamically collect all params 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 your intent is to pass it as a real JSON object so that you can use a JSON parser to break it further down into properties, then you have to convert it to a String before sending using JS/jQuery and specify the data argument as follows:

data: { "rooms": roomsAsString }

This way it's available as a JSON string by request.getParameter("rooms") which you can in turn parse using an arbitrary JSON API.


Unrelated to the concrete problem, don't use $ variable prefix in jQuery for non-jQuery objects. This makes your code more confusing to JS/jQuery experts. Use it only for real jQuery objects, not for plain vanilla strings or primitives.

var $foo = "foo"; // Don't do that. Use var foo instead.
var $foo = $("someselector"); // Okay.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top