转载自:http://blog.csdn.net/prostore/article/details/4205183受益匪浅
接下来将要写一个jsonList.JSP页面来将结果承现给用户
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>json 返回列表处理</title>
<script language="javascript">
var xmlHttp = false;
try{
xmlHttp = new ActiveXObject("msxml2.XMLHTTP");
}catch(e1){
try{
xmlHttp = new ActiveXObject("microsoft.XMLHTTP");
}catch(e2){
try{
xmlHttp = new XMLHttpRequest();
}catch(e3){
xmlHttp = false;
}
}
}
if(!xmlHttp){
alert("create xmlHttpRequest fail");
}
function jsonListResult(){
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4) {
var userList = eval('(' + xmlHttp.responseText + ')');
var user = eval(userList.JSONRESULT);
var result = "<table border=0>";
result += "<tr><td>userId</td><td>userName</td><td>sex</td></tr>";
for(var i=0;i<user.length;i++) {
result += "<tr>";
result += "<td>"+user[i].userId +"</td>";
result += "<td>"+user[i].userName+"</td>";
result += "<td>"+user[i].sex+"</td>";
result += "</tr>";
}
result += "</table>";
document.getElementById('jsonDiv').innerHTML = result;
}
}
xmlHttp.open("POST","jsonlist.action",true);
xmlHttp.send(null);
}
</script>
</head>
<body>
<div id="jsonDiv"></div>
<input type="button" value="show list" onclick="jsonListResult();">
</body>
</html>
[html] view plaincopy
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>json 返回列表处理</title>
<script language="javascript">
var xmlHttp = false;
try{
xmlHttp = new ActiveXObject("msxml2.XMLHTTP");
}catch(e1){
try{
xmlHttp = new ActiveXObject("microsoft.XMLHTTP");
}catch(e2){
try{
xmlHttp = new XMLHttpRequest();
}catch(e3){
xmlHttp = false;
}
}
}
if(!xmlHttp){
alert("create xmlHttpRequest fail");
}
function jsonListResult(){
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4) {
var userList = eval('(' + xmlHttp.responseText + ')');
var user = eval(userList.JSONRESULT);
var result = "<table border=0>";
result += "<tr><td>userId</td><td>userName</td><td>sex</td></tr>";
for(var i=0;i<user.length;i++) {
result += "<tr>";
result += "<td>"+user[i].userId +"</td>";
result += "<td>"+user[i].userName+"</td>";
result += "<td>"+user[i].sex+"</td>";
result += "</tr>";
}
result += "</table>";
document.getElementById('jsonDiv').innerHTML = result;
}
}
xmlHttp.open("POST","jsonlist.action",true);
xmlHttp.send(null);
}
</script>
</head>
<body>
<div id="jsonDiv"></div>
<input type="button" value="show list" onclick="jsonListResult();">
</body>
</html>