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字符串。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM