利用JackSon工具将JSON文件和对象互转
Posted 专注改变人生。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用JackSon工具将JSON文件和对象互转相关的知识,希望对你有一定的参考价值。
1. 需要下载JackSon工具,并导入到:
2. 编写html页面:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用户注册</title> <script type="text/javascript" src="./js/lib/jquery.js"></script> <script type="text/javascript"> /** * 验证两次输入密码是否一致 */ $(function(){ $(\'#yanzheng\').blur(function(){ if($(\'#passwd\')!=null){ //第一次输入的密码不为空 if($(\'#passwd\').val()==$(\'#yanzheng\').val()){ }else{ alert("两次输入的密码不一样"); } }else{ //第一次输入的密码为空 } }); }); </script> </head> <body> <div style="position:relative;left:50%"> <h1 style=>用户注册</h1></div> <div style="position:relative;left:45%"> <form action="servlet/regist" method="post"> 用户名:<input type="text" name="userName"/><br/> 密 码:<input type="password" name="passWd" id="passwd" /><br/> 确认密码:<input type="password" name="rePassWd" id="yanzheng"/><br/> 性 别:<input type="radio" name="sex" value="男" />男 <input type="radio" name="sex" value="女" />女<br/> <input type="submit" value="注册" /> </form> </div> </body> </html>
3. 编写 javabean
解析: bo是业务层,bo对象属性与用户输入的信息一一对应,本例用户输入的信息有userName、passWd、sex,所以javabean的User对象也有这三个属性。
package bo; public class User { private String userName; private String passWd; private String sex; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWd() { return passWd; } public void setPassWd(String passWd) { this.passWd = passWd; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
4. 编写servlet
package servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.fasterxml.jackson.databind.ObjectMapper; import bo.User; /** * Servlet implementation class Register */ public class Register extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public Register() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); //post方式用这种设置编码,如果是get方式,则用string.getBytes("ISO8859-1","utf-8")方法设置编码。 response.setContentType("text/html;charset=UTF-8"); //从浏览器获取用户信息 String userName=request.getParameter("userName"); String passWord=request.getParameter("passWd"); String sex=request.getParameter("sex"); //将用户信息装入javabean里 User user=new User(); user.setUserName(userName); user.setPassWd(passWord); user.setSex(sex); //将javabean转为json对象 ObjectMapper mapper = new ObjectMapper(); String json=mapper.writeValueAsString(user); PrintWriter out=response.getWriter(); out.print(json); out.close(); } }
结果:
以上是关于利用JackSon工具将JSON文件和对象互转的主要内容,如果未能解决你的问题,请参考以下文章
总结的一些json格式和对象/String/Map/List等的互转工具类