菜鸟学习Spring——SpringMVC注解版解析不同格式的JSON串

一、概述
        不同格式的JSON串传到后台来实现功能这个是我们经常要做的一件事,本篇博客就给大家介绍四种不同的JSON串传到后台后台如何用@RequestBody解析这些不同格式的JSON串的。
二、代码展示

需要引用的jar包


1.xml配置   Web.xml

 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <servlet>  
  7.         <servlet-name>springMVC</servlet-name>  
  8.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  9.         <load-on-startup>1</load-on-startup>  
  10.     </servlet>  
  11.     <servlet-mapping>  
  12.         <servlet-name>springMVC</servlet-name>  
  13.         <url-pattern>*.spring</url-pattern>  
  14.     </servlet-mapping>  
  15.     <welcome-file-list>  
  16.         <welcome-file>index.jsp</welcome-file>  
  17.     </welcome-file-list>  
  18. </web-app>  

springMVC-servlet.xml

<? xml   version = "1.0"   encoding = "UTF-8" ?>   
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="  
  5.         http://www.springframework.org/schema/beans   
  6.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.         http://www.springframework.org/schema/context   
  8.         http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  9.           http://www.springframework.org/schema/mvc   
  10.         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">  
  11.     <context:component-scan base-package="com.gaowei.JSON" />  
  12.     <mvc:annotation-driven />  
  13.       
  14.   
  15. </beans>  

2.Java代码  Userinfo.java

 
  1. package com.gaowei.entity;  
  2.   
  3. public class Userinfo {  
  4.     private String username;  
  5.     private String password;  
  6.     public String getUsername() {  
  7.         return username;  
  8.     }  
  9.     public void setUsername(String username) {  
  10.         this.username = username;  
  11.     }  
  12.     public String getPassword() {  
  13.         return password;  
  14.     }  
  15.     public void setPassword(String password) {  
  16.         this.password = password;  
  17.     }  
  18. }  

Test.java

 
  1. package com.gaowei.JSON;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.web.bind.annotation.RequestBody;  
  9. import org.springframework.web.bind.annotation.RequestMapping;  
  10.   
  11.  
  12. import com.gaowei.entity.Userinfo;  
  13.   
  14. @Controller  
  15. public class Test {  
  16.       
  17.     @RequestMapping(value="getJSON1")  
  18.     public void getJSON1(@RequestBody Userinfo userinfo){  
  19.         System.out.println("------getJSON1---start----");  
  20.         System.out.println(userinfo.getUsername());  
  21.         System.out.println(userinfo.getPassword());  
  22.         System.out.println("------getJSON1---end----");  
  23.     }  
  24.       
  25.     @RequestMapping(value="getJSON2")  
  26.     public void getJSON2(@RequestBody ArrayList<String> list){  
  27.         System.out.println("------getJSON2---start----");  
  28.         for (int i = 0; i < list.size(); i++) {  
  29.             System.out.println(list.get(i));  
  30.         }  
  31.         System.out.println("------getJSON2---end----");  
  32.     }  
  33.       
  34.     @RequestMapping(value="getJSON3")  
  35.     public void getJSON3(@RequestBody List<Map> list){  
  36.         System.out.println("------getJSON3---start----");  
  37.         for (int i = 0; i < list.size(); i++) {  
  38.             Map map=list.get(i);  
  39.             System.out.println(map.get("username")+" "+map.get("password"));  
  40.         }  
  41.         System.out.println("------getJSON3---end----");  
  42.     }  
  43.       
  44.     @RequestMapping(value="getJSON4")  
  45.     public void getJSON4(@RequestBody Map map){  
  46.         System.out.println("------getJSON4---start----");  
  47.         System.out.println(map.get("username"));  
  48.         List<Map> workList=(List)map.get("work");  
  49.         for (int i = 0; i < workList.size(); i++) {  
  50.             Map eachAddressMap=workList.get(i);  
  51.             System.out.println("address="+eachAddressMap.get("address"));  
  52.         }  
  53.         Map schoolMap=(Map)map.get("school");  
  54.         System.out.println(schoolMap.get("name"));  
  55.         System.out.println(schoolMap.get("address"));  
  56.         System.out.println("------getJSON4---end----");  
  57.     }  
  58. }  

3.界面代码 Test.jsp

 
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>  
  2.  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4.     <head>  
  5.         <script src="jquery-1.3.2.js">  
  6.         </script>  
  7.         <script src="json2.js">  
  8.         </script>  
  9.         <script>  
  10.             function userinfo(username, password){  
  11.                 this.username = username;  
  12.                 this.password = password;  
  13.             }  
  14.               
  15.             function sendAjax1(){  
  16.                 var userinfoRef = new userinfo('中国', '中国人');  
  17.                 var jsonStringRef = JSON.stringify(userinfoRef);  
  18.                 $.ajax({  
  19.                     type: "POST",  
  20.                     data: jsonStringRef,  
  21.                     url: "getJSON1.spring?t=" + new Date().getTime(),  
  22.                     contentType: "application/json"  
  23.                 });  
  24.             }  
  25.   
  26.             function sendAjax2(){  
  27.                 var myArray =new Array();  
  28.                 myArray[0]="中国1";  
  29.                 myArray[1]="中国2";  
  30.                 myArray[2]="中国3";  
  31.                 myArray[3]="中国4";  
  32.                 var jsonString=JSON.stringify(myArray);  
  33.                   $.ajax({  
  34.                         type: "POST",  
  35.                         data: jsonString,  
  36.                         url: "getJSON2.spring?t=" + new Date().getTime(),  
  37.                         contentType: "application/json"  
  38.                     });  
  39.                 }  
  40.   
  41.   
  42.             function sendAjax3(){  
  43.                     var myArray=new Array();  
  44.                     myArray[0]= new userinfo('中国1', '中国人1');  
  45.                     myArray[1]= new userinfo('中国2', '中国人2');  
  46.                     myArray[2]= new userinfo('中国3', '中国人3');  
  47.                     myArray[3]= new userinfo('中国4', '中国人4');  
  48.                     var jsonString=JSON.stringify(myArray);  
  49.                       $.ajax({  
  50.                             type: "POST",  
  51.                             data: jsonString,  
  52.                             url: "getJSON3.spring?t=" + new Date().getTime(),  
  53.                             contentType: "application/json"  
  54.                         });  
  55.                 }  
  56.   
  57.             function sendAjax4(){  
  58.                 var jsonObject={  
  59.                     "username":"accp",  
  60.                     "work":[{  
  61.                         "address":"address1"  
  62.                         },{  
  63.                         "address":"address2"      
  64.                             }],  
  65.                         "school":{  
  66.                             "name":"tc",  
  67.                             "address":"pjy"  
  68.                             }  
  69.                         }  
  70.                 var jsonString=JSON.stringify(jsonObject);  
  71.                   $.ajax({  
  72.                         type: "POST",  
  73.                         data: jsonString,  
  74.                         url: "getJSON4.spring?t=" + new Date().getTime(),  
  75.                         contentType: "application/json"  
  76.                     });  
  77.             }  
  78.         </script>  
  79.     </head>  
  80.     <body>  
  81.         <input type="button" onclick="sendAjax1()" value="sendAjax1"/>  
  82.         <br/>  
  83.         <input type="button" onclick="sendAjax2()" value="sendAjax2">  
  84.         <br/>  
  85.         <input type="button" onclick="sendAjax3()" value="sendAjax3">  
  86.         <br/>  
  87.         <input type="button" onclick="sendAjax4()" value="sendAjax4">  
  88.         <br/>  
  89.     </body>  
  90. </html>  

4.效果图



三、总结。

这里要注意jar包的引用不要把spring的所有jar包都引用了会引起jar包冲突而导致报HTTP 415的错误。@RequestBody这个方法很强大可以把JSON串转化为实体类、ArryList、Map等对象。这样的方法让我们开发人员开发效率大大的提高了

猜你喜欢

转载自nethub2.iteye.com/blog/2329361