jQuery调用Java产生的JSON示例

Androidize 贡献于2013-12-05

作者 lenovo  创建于2008-12-26 08:45:00   修改者lenovo  修改于2013-11-28 10:19:07字数1701

文档摘要:jQuery调用Java产生的JSON示例
关键词:

List list = new ArrayList(); list.add("zhangsan"); list.add("lisi"); list.add("wangwu"); JSONArray jarray = JSONArray.fromObject(list); System.out.println(jarray.toString()); Map map = new HashMap(); map.put("name", "zhangsan"); map.put("age", "20"); JSONObject obj = JSONObject.fromObject(map); System.out.println(obj.toString()); list.clear(); list.add(map); Map map1 = new HashMap(); map1.put("name", "li"); map1.put("age", "20"); list.add(map1); JSONArray jsonArray = JSONArray.fromObject(list); System.out.println(jsonArray.toString()); Student stu = new Student("001", "zhangsan", 20); JSONObject stuObj = JSONObject.fromObject(stu); System.out.println(stuObj.toString()); list.clear(); list.add(new Student("001", "zhangsan", 20)); list.add(new Student("002", "lisi", 23)); JSONArray stuAry = JSONArray.fromObject(list); System.out.println(stuAry.toString()); @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); //String num=req.getParameter(""); List list = new ArrayList(); list.add(new Student("001", "zhangsan", 20)); list.add(new Student("002", "lisi", 20)); list.add(new Student("003", "wangwu", 20)); list.add(new Student("004", "zhangliu", 20)); list.add(new Student("005", "ddd", 20)); JSONArray jsonArray = JSONArray.fromObject(list); PrintWriter out = resp.getWriter(); out.print(jsonArray.toString()); out.close(); } $(function(){ $("input[type='button']").click(function(){ $.get( "/jsp/myjson", function(data){ $.each(data,function(i,student){ var str = ""; str+=""+student.no+""; str+=""+student.name+""; str+=""+student.age+""; str+=""; $("table").append(str); }) },"json"); }); });

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 2 金币 [ 分享文档获得金币 ] 1 人已下载

下载文档