Json对象不在jsp中显示

时间:2016-04-10 06:50:57

标签: java json jsp servlets

我有一个jsp页面,我试图从我的servlet获取一个JSON对象。

jsp代码:

<%@page import="org.codehaus.jettison.json.JSONObject"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"   "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>View Json</title>
 <%
   JSONObject jsonObject=(JSONObject)request.getAttribute("jsonObject");
  %>
 </head>
 <body>
 <h6>JSON View</h6>
  <%=jsonObject%>
  </body>
 </html>

我的java代码将json对象发送到上面的jsp页面:

 JSONObject jsonObj = new JSONObject(jsonString.toString());
 request.setAttribute("jsonObject", jsonObj);
 RequestDispatcher dispatcher = request.getRequestDispatcher("check.html");
 dispatcher.forward(request, response);

我的jsp页面显示所有scriptlet而不是json数据。请指教。感谢。

我在jsp页面中看到了这个错误:

java.lang.ClassCastException: org.codehaus.jettison.json.JSONObject cannot be cast to org.json.simple.JSONObject

1 个答案:

答案 0 :(得分:1)

在jsp中更改import语句

<%@page import="org.codehaus.jettison.json.JSONObject"%>

<%@page import="org.json.simple.JSONObject"%>