求助 jsp 获取json格式数据 在线等
public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
ipImpl iI = new ipImpl();
ipbean ipjb = new ipbean();
String startDate = java.net.URLDecoder.decode(request.getParameter("startDate"), "UTF-8");
String endDate = java.net.URLDecoder.decode(request.getParameter("endDate"), "UTF-8");
List<ipbean> ipList = iI.queryIp(startDate, endDate);
// request.setAttribute("ipList", ipList);
Iterator<ipbean> itip = ipList.iterator();
JSONArray ja = new JSONArray();
while (itip.hasNext()) {
ipjb = itip.next();
JSONObject sign = new JSONObject();
sign.put("id", ipjb.getId());
sign.put("ip", ipjb.getStr_ip());
sign.put("Date", ipjb.getStr_Date());
ja.put(sign);
}
System.out.println(ja);
return null;
}
jsp页面上如何按表格格式显示获取到ja里的数据
------解决方案--------------------
你后台先response....print(ja);
前台JSP才可以得到JSON数据.
前台通过response.responseText 可以得到JSON 后面的处理自己看看如何读取JSON数据
------解决方案--------------------
接你的代码:
response.setContentType("text/plain;charset=utf-8");
PrintWriter responseStream = response.getWriter();
responseStream.println(jsonArray);
------解决方案--------------------
Java code
@RequestMapping("/topic/by_channel.do")
public void topicsByChannel(Integer channelId, HttpServletResponse response)
throws JSONException {
JSONArray arr = new JSONArray();
if (channelId != null) {
List<CmsTopic> list = manager.getListByChannel(channelId);
JSONObject o;
for (CmsTopic t : list) {
o = new JSONObject();
o.put("id", t.getId());
o.put("name", t.getName());
arr.put(o);
}
}
ResponseUtils.renderJson(response, arr.toString());
}
------解决方案--------------------
Java code
/**
* 发送json。使用UTF-8编码。
*
* @param response
* HttpServletResponse
* @param text
* 发送的字符串
*/
public static void renderJson(HttpServletResponse response, String text) {
render(response, "application/json;charset=UTF-8", text);
}