• jackson使用方法总结


     一、转换java对象为json

    第一步:创建Jackson的核心对象 ObjectMapper

    第二步:使用转换方法

    常用的有:

    1、writeValue(参数一,参数二);

    参数一:

      (1) File:将obj对象转换为JSON字符串,并保存到指定的文件中

      (2) Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中(通常可以使用respon的getWrite()方法,获取该类)

      (3) OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中

    参数二:
      要转换为JSON的对象 

    2、writeValueAsString(obj):将对象转为json字符串


    示例代码:

     1 @WebServlet("/testJsonServlet")
     2 public class TestJsonServlet extends HttpServlet {
     3     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     4 
     5         Map<String, String[]> map = request.getParameterMap();
     6         //使用BeanUtils封装TestUser
     7         TestUser testUser = new TestUser();
     8         try {
     9             BeanUtils.populate(testUser, map);
    10         } catch (IllegalAccessException e) {
    11             e.printStackTrace();
    12         } catch (InvocationTargetException e) {
    13             e.printStackTrace();
    14         }
    15         System.out.println(testUser);
    16 
    17         ObjectMapper mapper = new ObjectMapper();
    18         response.setContentType("application/json;charset=utf-8");
    19         mapper.writeValue(response.getWriter(),testUser);
    20     }
    21 
    22     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    23         this.doPost(request, response);
    24     }
    25 }

    访问 /testJsonServlet?name=tom&phone=123

    页面返回的内容为

    说明服务器成功将参数以json的格式发送到客户端。

    二、转换list集合为json

    与将java对象转换为json格式大同小异,这里是将obj替换为list集合对象

    看一下代码:

     1 @WebServlet("/testJsonServlet")
     2 public class TestJsonServlet extends HttpServlet {
     3     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     4 
     5 /*
     6         Map<String, String[]> map = request.getParameterMap();
     7         //使用BeanUtils封装TestUser
     8         TestUser testUser = new TestUser();
     9         try {
    10             BeanUtils.populate(testUser, map);
    11         } catch (IllegalAccessException e) {
    12             e.printStackTrace();
    13         } catch (InvocationTargetException e) {
    14             e.printStackTrace();
    15         }
    16         System.out.println(testUser);
    17 
    18         ObjectMapper mapper = new ObjectMapper();
    19         response.setContentType("application/json;charset=utf-8");
    20         mapper.writeValue(response.getWriter(),testUser);
    21 */
    22 
    23         TestUser testUser1 = new TestUser();
    24         testUser1.setName("tom");
    25         testUser1.setPhone(123);
    26 
    27         TestUser testUser2 = new TestUser();
    28         testUser2.setName("tom");
    29         testUser2.setPhone(123);
    30 
    31         TestUser testUser3 = new TestUser();
    32         testUser3.setName("tom");
    33         testUser3.setPhone(123);
    34 
    35         List<TestUser> list = new ArrayList<TestUser>();
    36         list.add(testUser1);
    37         list.add(testUser2);
    38         list.add(testUser3);
    39 
    40         ObjectMapper mapper = new ObjectMapper();
    41         response.setContentType("application/json;charset=utf-8");
    42         String json = mapper.writeValueAsString(list);
    43         response.getWriter().write(json);
    44 
    45     }
    46 
    47     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    48         this.doPost(request, response);
    49     }
    50 }

    访问 /testJsonServlet

    此时返回的是一个数组,数组中为三个json对象 

    三、转换Map集合为json

    同样是将对象换做map集合,

    代码如下:

     1 @WebServlet("/testJsonServlet")
     2 public class TestJsonServlet extends HttpServlet {
     3     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     4 
     5         TestUser testUser1 = new TestUser();
     6         testUser1.setName("tom");
     7         testUser1.setPhone(123);
     8 
     9         TestUser testUser2 = new TestUser();
    10         testUser2.setName("tom");
    11         testUser2.setPhone(123);
    12 
    13         TestUser testUser3 = new TestUser();
    14         testUser3.setName("tom");
    15         testUser3.setPhone(123);
    16 
    17         List<TestUser> list = new ArrayList<TestUser>();
    18         list.add(testUser1);
    19         list.add(testUser2);
    20         list.add(testUser3);
    21 
    22         Map<Integer, TestUser> map = new HashMap<Integer, TestUser>();
    23         map.put(1,testUser1);
    24         map.put(2,testUser2);
    25         map.put(3,testUser3);
    26 
    27         ObjectMapper mapper = new ObjectMapper();
    28         response.setContentType("application/json;charset=utf-8");
    29         String json = mapper.writeValueAsString(map);
    30         response.getWriter().write(json);
    31     }
    32 
    33     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    34         this.doPost(request, response);
    35     }
    36 }

    访问 /testJsonServlet

    结果如下图

    返回的内容为,json对象,其中每个键值对应的value值也是一个json对象


    PS

    response.setContentType(MIME)的作用是使客户端浏览器,区分不同种类的数据,并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据。
    例如web浏览器就是通过MIME类型来判断文件是GIF图片。通过MIME类型来处理json字符串。

  • 相关阅读:
    jhljx跑跑跑(找规律)
    FFT教你做乘法(FFT傅里叶变换)
    寻找最远点对(凸包求解)
    捡火柴的Nova君(n个线段相交问题)
    生命游戏/Game of Life的Java实现
    如果看了此文你还不懂傅里叶变换,那就过来掐死我吧【完整版】(转)
    北航第十一届程序设计竞赛网络预赛题解
    LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
    UVa 112
    LeetCode 2 Add Two Numbers(链表操作)
  • 原文地址:https://www.cnblogs.com/justn0w/p/11181274.html
Copyright © 2020-2023  润新知