我是 json 新手,我收到 json 对象格式的请求。
{"login_detail":[{"password":"world","username":"hello"}]}
我想通过 servlet 解析它,我的代码如下 -
public class LoginServlet extends HttpServlet{
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//String json=request.getParameter("x");
try {
String jsonreq=request.getParameter("x");
System.out.println(jsonreq);
JSONArray outerArray = (JSONArray) JSONSerializer.toJSON(jsonreq);
JSONObject json1 = (JSONObject) outerArray.get(0);
JSONArray jarray1 = json1.getJSONArray("login_detail");
for (int i = 0; i < jarray1.size(); i++)
{
JSONObject hotel = jarray1.getJSONObject(i);
String user = hotel.getString("pasword");
System.out.println(user);
String pass=hotel.getString("username");
System.out.println(pass);
}
out.print(jsonreq);
}catch(Exception e){
System.out.println("inside exception");
e.printStackTrace();
}
finally {
out.close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}}
但我得到了一个例外
Sep 09, 2013 4:57:52 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet login threw exception
java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1850)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:890)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1354)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
at com.serverside.LoginServlet.processRequest(LoginServlet.java:40)
at com.serverside.LoginServlet.doPost(LoginServlet.java:72)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:722)
现在我正在使用 json-lib 2.2.2 jar 文件。从 android 客户端接收请求没有任何问题。我无法理解 JSONObject 参数化构造函数是否在 ANDROID 中完美运行,那么为什么它不适用于 servlet。??什么问题我无法理解。如果有人会帮助我,我将非常感激。