json_lib 和jackson 案列

来源:互联网 发布:数据分析属于it么 编辑:程序博客网 时间:2024/06/05 22:56

Jackson这三个包必须

jackson-core-2.2.3.jar(核心jar包

jackson-annotations-2.2.3.jar(该包提供Json注解支持

jackson-databind-2.2.3.jar





案列:

public class ServletToJackson extends HttpServlet {
private JdbcDaoImpl jdbcDao = new JdbcDaoImpl();


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();


// 获取页面提交的数据
System.out.println(request.getParameter("uname"));
System.out.println(request.getParameter("age"));
System.out.println(request.getParameter("upass"));
// 从后天获取数据

//1.JSON_LIB的写法


List<UserLogin> list = jdbcDao.getAll();

//把结合转成json

/*JSONArray jsonarray = JSONArray.fromObject(list);*/


// 把单个对象转化成json格式的对象
/* JSONObject json = JSONObject.fromObject(list); */

//json对象转化成java对象

List<UserLogin> list2 =JSONArray.toList(jsonarray);

//json_lib 把json对象转换成java 对象比较复杂 用的时候查查资料

//2、jackson 操作的方法
// 把list集合转换成json格式的数据
ObjectMapper json = new ObjectMapper();
UserLogin user = list.get(0);

//单个的对象的操作
String jsonOne= json.writeValueAsString(user);

//集合的操作
String jsonObject = json.writeValueAsString(list);

// json控制台输出
System.out.println(jsonObject);
System.out.println(jsonOne);

//单个对象的反序列化
ObjectMapper json1 = new ObjectMapper();
UserLogin user1 =json.readValue(jsonOne, UserLogin.class);
//对list集合的序列化
List<UserLogin> list2 = json.readValue(jsonObject,new TypeReference<List<UserLogin>>() {
});
}


}



页面代码:ajax post请求:


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


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">


<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
</head>
<script type="text/javascript">
$(function() {
$("#btn").click(function() {

$.ajax({
type : "post",
url : "http://localhost:8089/objectToJson/servletToJackson",
data : {
"uname" : "jack",
"upass" : "12345",
"age" : 20
},
success : function(data) {
var json = eval("("+ data +")");
var str="";
for(var i=0;i<json.length;i++){
str +='用户名:<input type="text" name="uname" value="' +json[i].uname+ '"><br>';
str+=' 密码:<input type="text" name="upass" value="'+json[i].upass+'"><br>';
str+=' 性别:<input type="text" name="sex" value="'+json[i].sex+'"><br>';
str+=' 年龄:<input type="text" name="age" value="'+json[i].age+'"><br>';
str+=' 地址:<input type="text" name="address" value="'+json[i].address+'"><br>';
str+=' 电话:<input type="text" name="telephone" value="'+json[i].telephone+'"><br>';
str+='<hr><br>';
};
$("#div").attr("align","center").html(str);


}


});


});


});
</script>


<body>


<div id="div">



<input type="button" name="button" id="btn" value="提交">
</div>


<br>
</body>
</html>


0 0
原创粉丝点击