🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 一、概述 不同格式的JSON串传到后台来实现功能这个是我们经常要做的一件事,本篇博客就给大家介绍四种不同的JSON串传到后台后台如何用@RequestBody解析这些不同格式的JSON串的。 ## 二、代码展示 需要引用的jar包 ![](https://box.kancloud.cn/2016-02-22_56caddfd20a2b.jpg) 1.xml配置 Web.xml ~~~ <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>*.spring</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> ~~~ springMVC-servlet.xml ~~~ <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <context:component-scan base-package="com.gaowei.JSON" /> <mvc:annotation-driven /> </beans> ~~~ 2.java代码 Userinfo.java ~~~ package com.gaowei.entity; public class Userinfo { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } ~~~ Test.java ~~~ package com.gaowei.JSON; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import com.gaowei.entity.Userinfo; @Controller public class Test { @RequestMapping(value="getJSON1") public void getJSON1(@RequestBody Userinfo userinfo){ System.out.println("------getJSON1---start----"); System.out.println(userinfo.getUsername()); System.out.println(userinfo.getPassword()); System.out.println("------getJSON1---end----"); } @RequestMapping(value="getJSON2") public void getJSON2(@RequestBody ArrayList<String> list){ System.out.println("------getJSON2---start----"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } System.out.println("------getJSON2---end----"); } @RequestMapping(value="getJSON3") public void getJSON3(@RequestBody List<Map> list){ System.out.println("------getJSON3---start----"); for (int i = 0; i < list.size(); i++) { Map map=list.get(i); System.out.println(map.get("username")+" "+map.get("password")); } System.out.println("------getJSON3---end----"); } @RequestMapping(value="getJSON4") public void getJSON4(@RequestBody Map map){ System.out.println("------getJSON4---start----"); System.out.println(map.get("username")); List<Map> workList=(List)map.get("work"); for (int i = 0; i < workList.size(); i++) { Map eachAddressMap=workList.get(i); System.out.println("address="+eachAddressMap.get("address")); } Map schoolMap=(Map)map.get("school"); System.out.println(schoolMap.get("name")); System.out.println(schoolMap.get("address")); System.out.println("------getJSON4---end----"); } } ~~~ 3.界面代码 Test.jsp ~~~ <%@ page language="java" import="java.util.*" pageEncoding="utf-8" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script src="jquery-1.3.2.js"> </script> <script src="json2.js"> </script> <script> function userinfo(username, password){ this.username = username; this.password = password; } function sendAjax1(){ var userinfoRef = new userinfo('中国', '中国人'); var jsonStringRef = JSON.stringify(userinfoRef); $.ajax({ type: "POST", data: jsonStringRef, url: "getJSON1.spring?t=" + new Date().getTime(), contentType: "application/json" }); } function sendAjax2(){ var myArray =new Array(); myArray[0]="中国1"; myArray[1]="中国2"; myArray[2]="中国3"; myArray[3]="中国4"; var jsonString=JSON.stringify(myArray); $.ajax({ type: "POST", data: jsonString, url: "getJSON2.spring?t=" + new Date().getTime(), contentType: "application/json" }); } function sendAjax3(){ var myArray=new Array(); myArray[0]= new userinfo('中国1', '中国人1'); myArray[1]= new userinfo('中国2', '中国人2'); myArray[2]= new userinfo('中国3', '中国人3'); myArray[3]= new userinfo('中国4', '中国人4'); var jsonString=JSON.stringify(myArray); $.ajax({ type: "POST", data: jsonString, url: "getJSON3.spring?t=" + new Date().getTime(), contentType: "application/json" }); } function sendAjax4(){ var jsonObject={ "username":"accp", "work":[{ "address":"address1" },{ "address":"address2" }], "school":{ "name":"tc", "address":"pjy" } } var jsonString=JSON.stringify(jsonObject); $.ajax({ type: "POST", data: jsonString, url: "getJSON4.spring?t=" + new Date().getTime(), contentType: "application/json" }); } </script> </head> <body> <input type="button" onclick="sendAjax1()" value="sendAjax1"/> <br/> <input type="button" onclick="sendAjax2()" value="sendAjax2"> <br/> <input type="button" onclick="sendAjax3()" value="sendAjax3"> <br/> <input type="button" onclick="sendAjax4()" value="sendAjax4"> <br/> </body> </html> ~~~ 4.效果图 ![](https://box.kancloud.cn/2016-02-22_56caddfd3ba79.jpg) ![](https://box.kancloud.cn/2016-02-22_56caddfd55b4b.jpg) ## 三、总结。 这里要注意jar包的引用不要把Spring的所有jar包都引用了会引起jar包冲突而导致报HTTP415的错误。@RequestBody这个方法很强大可以把JSON串转化为实体类、ArryList、Map等对象。这样的方法让我们开发人员开发效率大大的提高了