<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 一、概述 不同格式的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等對象。這樣的方法讓我們開發人員開發效率大大的提高了
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看