解析JSON的Servlet中使用JSON-简单 [英] Parse JSON in Servlet with json-simple

查看:367
本文介绍了解析JSON的Servlet中使用JSON-简单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建并发送一个JSON对象与jQuery,但我无法弄清楚如何使用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的code是如下:

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(),并打印出来给我的控制台,我得到的参数名称客房[0] [关键] 诸如此类,但我无法解析JSON阵列室以任何方式。我试图通过解析的request.getParameter(房间) .getParameterValues​​(房间)变种,但他们都返回空值。

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数据或方式有没有办法解析JSON在我失踪的servlet的?

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?

要求更多code,即使该servlet仍然是pretty的很多空的,因为我无法弄清楚如何分析数据。

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

推荐答案

数据 $的说法。阿贾克斯()需要重新presenting的请求参数映射一个JS对象。所以你喂给它的任何的JS对象将被转换以请求参数。既然你传递的JS对象纯香草它,它会被视为一个请求参数映射。您需要通过重新presentation,而不是完全符合他们的要求参数名称来访问各个参数。

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

您甚至可以动态地收集所有PARAMS如下:

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解析器来打破它进一步下降到属性,那么你必须把它转换为一个字符串使用JS / jQuery和指定数据参数如下发送前:

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 }

这种方式是可以作为一个JSON字符串由的request.getParameter(房间),你可以反过来解析使用任意的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的对象使用 $ 变量preFIX。这使你的code较为混乱,以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.

这篇关于解析JSON的Servlet中使用JSON-简单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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