我有一个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
最佳答案
在jsp中更改导入语句
从
<%@page import="org.codehaus.jettison.json.JSONObject"%>
至
<%@page import="org.json.simple.JSONObject"%>