求一段ajax通过json传递数组时的后台代码,跟前台就收json并解析的代码

求一段ajax通过json传递数组时的后台代码,和前台就收json并解析的代码
  求一段ajax通过json传递数组时的后台代码,和前台就收json并解析的代码,没弄过,想研究一下,但是网上搜不到合适的,都是用jquery弄得,我想要不用jquery的
所有分都给你 如果给我的代码合适

------解决方案--------------------

public class Test3 {

public static void main(String[] args) {
String json = "{'num':11,'org':{'orgId':'orgId','orgName':'orgName'},'biz':" +
"[{'appcode':55,'subscode':'subscode0'},{'appcode':66,'subscode':'subscode1'}]}";
JSONObject jo = JSONObject.fromObject(json);
System.out.println(jo.get("num"));
JSONObject orgJson= jo.getJSONObject("org");
System.out.println(orgJson);
System.out.println(orgJson.get("orgId"));
JSONArray bizJson= jo.getJSONArray("biz");
System.out.println(bizJson.getJSONObject(0).get("appcode"));
}

}


// 下面是输出结果
11
{"orgId":"orgId","orgName":"orgName"}
orgId
55

实现上面的代码功能,还需要加入某些包:
  commons系列的包,可在网站:http://www.docjar.com/上面搜索下载,其它包可下载网站如下:
  http://json-lib.sourceforge.net/
  http://ezmorph.sourceforge.net/
http://morph.sourceforge.net/
------解决方案--------------------
后台servlet:
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletExceptionIOException {
        req.setCharacterEncoding("UTF-8");
        ChatDao cd=new ChatDao();
        List<Chat> chatList=new ArrayList<Chat>();
        chatList=cd.chatList();
        JSONArray json=JSONArray.fromObject(chatList);
        resp.setContentType("text/html;charset=UTF-8");
     PrintWriter out=resp.getWriter();
out.println(json.toString());
}


前台js:
<script type="text/javascript">
var xhq;
var input;

//创建XMLHttpRequest对象
function createXMLHttpRequest(){
  if(window.XMLHttpRequest){
     xhq=new XMLHttpRequest();
  }else if(window.ActiveXObject){
     try{
        xhq=new ActiveXObject("Msxml2.XMLHTTP");
     }catch(e){
        try{
           xhq=new ActiveXObject("Microsoft.XMLHTTP");
        }catch(e){
        
        }
     }
  }

}

//发送数据到服务器
function sendRequest(){
  createXMLHttpRequest();
  var url="ChatServlet";
  //通过open方法取得与服务器的连接
  //发送post请求
  xhq.open("POST",url,true);
  //设置请求头,发送post时需要该请求头
  xhq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xhq.onreadystatechange=processResponse;
  xhq.send(null);
  
}

function processResponse(){
   if(xhq.readyState==4){
      if(xhq.status==200){
         var chatList=eval("("+xhq.responseText+")");
         var chat1="";
         var chat2;
         var id;
         
         for(var i=0;i<chatList.length;i++){
              id=i+1;
              chat2="<tr><td width='25%'>"+id+"</td>"+"<td width='25%'>"+chatList[i].owner+"</td>"+"<td width='25%'>"+chatList[i].content+"</td></tr>";
              chat1=chat1+chat2;
         }
         document.getElementById("chat").innerHTML=chat1;
      }else{
        window.alert("您所请求的页面异常!");
      }   
   }
}
</script>