客户端代码: <script language="javascript" type="text/javascript" src="json.js"></script> <script type="text/javascript"> function getJSON(obj){ var boy = obj.toJSONString(); var boyName = obj.name; var boyAge = obj.age; alert(boyName+" "+boyAge); } </script> <script src="http://localhost:8080/jsonExa/servlet/Server1?callback=getJSON" type="text/javascript"> </script> 服务器端: public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); String callbackFunc = request.getParameter("callback"); PrintWriter out = response.getWriter(); String resJSON = "{'name':'jj', 'age':20}"; // System.out.println(callbackFunc+"("+resJSON+")"); //JSONObject jsonObject = JSONObject.fromString(resJSON); JSONObject resultJSON; try { resultJSON = new JSONObject(resJSON); System.out.println(callbackFunc+"("+resultJSON+")"); out.println(callbackFunc+"("+resultJSON+")"); out.flush(); out.close(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } }