gpt4 book ai didi

java - Json对象不显示在jsp中

转载 作者:行者123 更新时间:2023-12-01 10:05:12 25 4
gpt4 key购买 nike

我有一个 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"%>

关于java - Json对象不显示在jsp中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36526811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com