Java objects, List collection of turn json

 1 @RequestMapping("/editReceiver")
 2     public void editReceiver(HttpServletRequest request,HttpServletResponse response) {
 3         int id = Integer.parseInt(request.getParameter("id"));
 4         //System.out.println(id);
 5         ReceiverInfo receiverInfo =receiverService.editRInfo(id);
 6         //生成JSON: 
 7         Gson gson =new  Gson();
 8         String json = gson.toJson(receiverInfo);
 9         response.setCharacterEncoding("UTF-8");  
10         response.setContentType("application/json; charset=utf-8");  
11         PrintWriter writer = null;
12         try {
13             writer = response.getWriter();
14         } catch (IOException e) {
15             e.printStackTrace();
16         }
17         writer.append(json);
18     }
. 1 @RequestMapping ( "/ JianSuanList" )
 2      public  void jianSuanList (the HttpServletRequest Request, the HttpServletResponse Response) {
 . 3          the HttpSession the session = Request.getSession ();
 . 4          @SuppressWarnings ( "an unchecked") // for canceling the warning generated by some compilers occlusion of the left side of the ranks of the code, and sometimes it will block we hit the breakpoint debugging breakpoint 
5          List <LandCart> laCartsList = (List <LandCart>) session.getAttribute ( "List" );
 6          Gson GSON = new new Gson ();
 . 7          String JSON = gson.toJson (laCartsList);
 . 8          response.setCharacterEncoding ( "UTF-. 8" );  
 9         response.setContentType("application/json; charset=utf-8");  
10         PrintWriter writer=null;
11         try {
12             writer=response.getWriter();
13         } catch (IOException e) {
14             e.printStackTrace();
15         }
16         writer.append(json);    
17     }

We need to rely on import com.google.gson.Gson  

 <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
  <dependency>
     <groupId>com.google.code.gson</groupId>
     <artifactId>gson</artifactId>
     <version>2.8.2</version>
 </dependency>

 

Guess you like

Origin www.cnblogs.com/smart-fox/p/11365092.html