求助 jsp 获取json格式数据 在线等

li1037 2012-05-11 09:08:36
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里的数据
...全文
903 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hugo000002020 2012-05-11
  • 打赏
  • 举报
回复

/**
* 发送json。使用UTF-8编码。
*
* @param response
* HttpServletResponse
* @param text
* 发送的字符串
*/
public static void renderJson(HttpServletResponse response, String text) {
render(response, "application/json;charset=UTF-8", text);
}


hugo000002020 2012-05-11
  • 打赏
  • 举报
回复

@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());
}




/**
* 发送内容。使用UTF-8编码。
*
* @param response
* @param contentType
* @param text
*/
public static void render(HttpServletResponse response, String contentType,
String text) {
response.setContentType(contentType);
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
try {
response.getWriter().write(text);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}



$(function() {
submitHandler : function(form) {
$(form).ajaxSubmit( {
"success" : function(data) {
test1(data);
},
"dataType" : "json"
});
}
});
});
function test1(data){
//var jsonStr='[{"id":1,"name":"name1"},{"id":2,"name":"name2"},{"id":3,"name":"name3"},{"id":4,"name":"name4"},{"id":5,"name":"name5"}]';
var jsonStr=data;
var list=eval("("+jsonStr+")");
var tabs='<table id="tb1" border="1">';
var theads='<tr><td>ID</td><td>name</td></tr>';
var trs="";
for ( var i = 0; i < list.length; i++) {
var tr='';
tr+='<tr>';
tr+='<td>'+list[i].name+'</td>';
tr+='<td>'+list[i].name+'</td>';
tr+='</tr>';
trs+=tr;
}
tabs+=theads;
tabs+=trs;
tabs+='</table>';
alert(tabs);
var div1=document.getElementById('div1');
if(div1){
div1.innerHTML=tabs;
}
}


一般像这种我们是用ajax获得后台返回的json字符串,然后根据返回结果我们再封装,这里你稍威改一下
_jerrytiger 2012-05-11
  • 打赏
  • 举报
回复
jsp传递json对象

比如 传给你的是{"a":"a" , "b" : "123" , "c" : "hello"} 这样的一个json对象
你用的是Struts吧 ,
你可以定义 String a , b , c ; 然后给他们声称set方法
struts就能够给他们赋值了。

也可以通过ServletActionContex.getContex().getRequest().getParamt..("a");
//方法名都记不清,你自己去查
古布 2012-05-11
  • 打赏
  • 举报
回复
接你的代码:
response.setContentType("text/plain;charset=utf-8");
PrintWriter responseStream = response.getWriter();
responseStream.println(jsonArray);
ipromise_u 2012-05-11
  • 打赏
  • 举报
回复
你后台先response....print(ja);

前台JSP才可以得到JSON数据.
前台通过response.responseText 可以得到JSON 后面的处理自己看看如何读取JSON数据
任第一 2012-05-11
  • 打赏
  • 举报
回复
第一步:先创建一个jsp页面。写好<tbody>;

<table>
<tbody id="contentTbody">
</tbody>
</table>

第二步:1、将返回的json格式的数据循环写出来。
function setUserList(list, page) {
var sb = new StringBuffer();
$.each(list, function(i, item) {
sb.append('<tr>\n');
sb.append('<td align="right">').append(i+1).append('</td>\n');
sb.append('<td>').append(item.title).append('</td>\n');
sb.append('</tr>\n');
});
$("#contentTbody").html(sb.toString());

}
2、用forEach语句写出来上网搜一下就Ok了。
wangkai221 2012-05-11
  • 打赏
  • 举报
回复
jQuery

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧