如何在Servlet request.getParameter中从jQuery读取复杂的JSON对象 [英] How to read complex JSON object from jQuery in Servlet request.getParameter

查看:165
本文介绍了如何在Servlet request.getParameter中从jQuery读取复杂的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery创建和发送JSON对象,但是我不知道如何使用org.json.simple库在我的Ajax servlet中正确解析它.

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.

我的jQuery代码如下:

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";
           }
       });

在servlet中,当我使用request.getParameterNames()并将其打印到控制台时,得到的参数名称为rooms[0][key]等,但是我无法以任何方式解析JSON Array房间.我尝试解析由request.getParameter("rooms").getParameterValues("rooms")变体返回的对象,但是它们都返回一个空值.

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.

我在jQuery中格式化JSON数据的方式是否存在问题,或者是否有办法解析我所缺少的servlet中的JSON?

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?

要求更多代码,即使servlet仍然很空,因为我无法弄清楚如何解析数据.

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

推荐答案

$.ajax()data自变量采用表示请求参数映射的JS对象.因此,您提要给它的任何JS对象都将转换为请求参数.由于您要向其传递普通的JS对象,因此将其视为请求参数映射.您需要通过确切地使用它们的请求参数名称表示形式来访问各个参数.

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]");
// ...

您可以通过找到它们HttpServletRequest#getParameterMap() 方法:

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]");
    // ...
}

如果您打算将其作为真实的JSON对象传递,以便可以使用JSON解析器将其进一步分解为属性,则必须先将其转换为String,然后再使用JS/jQuery发送并指定data参数如下:

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 }

通过这种方式,request.getParameter("rooms")可以将其作为JSON字符串使用,您可以依次使用任意JSON API对其进行解析.

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

无关与具体问题无关,请勿在jQuery中为非jQuery对象使用$变量前缀.这使您的代码对JS/jQuery专家更加混乱.仅将其用于真正的jQuery对象,而不能用于普通的普通字符串或基元.

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.

这篇关于如何在Servlet request.getParameter中从jQuery读取复杂的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆