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
}
}
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
}
}
相关文章推荐
- 网络传输中,对Json组织方式的数据的解析以及使用这个方法,模拟从服务器端下载数据
- Retrofit2+RxJava2 使用POST方式提交JSON数据,服务器无法解析的问题
- json解析的几种方式
- 关于iOS开发 Json解析的几种方式
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
- JavaScript中解析JSON --- json.js 、 json2.js 以及 json3.js的使用区别
- string解析为json的几种方式(转)
- Spring定时任务的实现方式--ScheduledExecutorService and ScheduledExecutorFactoryBean的简单源码解析以及使用
- 关于SAXReader解析XML以及JSONObject和JSONArray的使用总结
- AsyncTask的使用以及Json解析
- Android 使用Gson解析json案例详解
- 关于Entity Framework更新的几种方式以及可能遇到的问题(附加类型“Model”的实体失败,因为相同类型的其他实体已具有相同的主键值)在使用 "Attach" 方法或者将实体的状态设置为 "Unchanged" 或 "Modified" 时如果图形中的任何实体具有冲突键值,则可能会发生上述行为
- 使用ListView和AsyncTask、fastjson解析Json以及适配器BaseAdapter来实现下载网络的图片以及文字并显示出来
- spring ioc容器注入的集中方式,以及属性注入的集中依赖,继承,使用SPEL表达式,ioc容器的生命周期,bean的几种工厂方法配置方式,bean的自动装配
- 解决android使用gson解析json字符串,并使用混淆编译方式打包apk遇到的问题
- JSON的几种解析方式的比较
- iOS- JSon和Xml解析,与服务器交互数据的解析详解与使用,各种解析方式详解
- json字符串解析 以及 组成json数据的方式
- json解析的几种方式
- struts2的配置解释以及使用ajax、创建传输json的两种方式