您的位置:首页 > Web前端 > JavaScript

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

2013-04-17 09:24 856 查看
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
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐