json使用案例以及几种解析json方式


http://www.cnblogs.com/hl0071/articles/1360321.html


myPagination分页插件使用案例:后台使用fastjson-1.1.26.jar解析json


前台代码result.jsp:

<%@page import="com.zjlolife.util.PageModel"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
  PageModel pageModel = (PageModel)request.getAttribute("pageModel");

  
 %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
     <base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>myPaginationV4.0 Jquery Plug-in --- LinApex</title>
<link href="css/page.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.6.js" type="text/javascript">
    </script>
<script src="js/jquery.myPagination.js" type="text/javascript">      
    </script>
<script>
 $(document).ready(function() {
    $("#demo").myPagination({
      currPage: 1,
      pageCount: <%=pageModel.getPageCount()%>,
      pageSize: 5,
      ajax: {
              on: true,                        //开启状态
              callback: 'ajaxCallBack',    //回调函数,注,此 ajaxCallBack 函数,必须定义在 $(function() {}); 外面
              url: "<%=basePath%>servlet/Servlet",            //访问服务器地址
              dataType: 'json',                //返回类型
              param:{on:true,page:1,pageSize:5}	//参数列表,其中  on 必须开启,page 参数必须存在,其他的都是自定义参数,如果是多条件查询,可以序列化表单,然后增加 page 参数
            }
    });
  });

//自定义 回调函数
function ajaxCallBack(data) {
        // alert(data.result);   //显示服务器返回信息
         
         var insetViewData = "";	//视图数据
         
         var result = eval("("+data.result+")");
       
         var students = result.students;
         $.each(students, function(i) {
        	insetViewData += createTR(i,students[i]);
         });
         
         $("table > tbody").html(insetViewData);
}

function createTR(key,value){
	var tr = "<tr>";
	tr += "<td>"+value.id+"</td>";
	tr += "<td>"+value.name+"</td>";
	tr += "<td>"+value.age+"</td>";
	tr += "<td>"+value.teacherName+"</td>";
	tr += "<td>"+value.yuanxi+"</td>";
	tr += "</tr>";
	return tr;
}
</script>
</head>
  
  <body>
  
     <div style="margin:0px auto 0px;width: 300;height:200;color: black;">
     <table width="300" border="1">
  <thead>
      <tr>
         <td>学号</td>
         <td>学生姓名</td>
         <td>年龄</td>
         <td>老师</td>
         <td>院系</td>
      </tr>
  </thead>
  <tbody>
  </table>
  </div>

  
   <div id="demo"></div>
  </body>
</html>

后台代码:(封装的PageModel转换成json对象,输出带前台,还使用了ajax技术)

package com.zjlolife.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zjlolife.util.PageModel;
import com.zjlolife.util.PageService;

public class Servlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Servlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		System.out.println(request.getParameter("page"));
		 int pageNo = 1;
		 if(request.getParameter("page")!=null) {
			 pageNo = Integer.valueOf(request.getParameter("page"));
		 }
		 int pageSize  = 5;
		 if(request.getParameter("pageSize")!=null) {
			 pageSize = Integer.valueOf(request.getParameter("pageSize"));
		 }
         
        JSONObject jsonObject = new JSONObject();
	    PageModel pageModel = new PageService().findAllStudents(pageNo, pageSize);
	    String json = JSON.toJSONString(pageModel);
	    System.out.println(json);
	    jsonObject.put("result", json);
	    System.out.println(jsonObject);
	    response.setCharacterEncoding("utf-8");//使用ajax技术的时候,此处最好加上response编码,否则会出现乱码
	    response.getWriter().print(jsonObject);
	    
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
    this.doGet(request, response);
	
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值