ÎÄ×Ö
A A
ĬÈÏ »¤ÑÛ Ò¹¼ä
Ò»¡¢¸ÅÊö
²»Í¬¸ñʽµÄJSON´®´«µ½ºǫ́À´ÊµÏÖ¹¦ÄÜÕâ¸öÊÇÎÒÃǾ³£Òª×öµÄÒ»¼þÊ£¬±¾Æª²©¿Í¾Í¸ø´ó¼Ò½éÉÜËÄÖÖ²»Í¬µÄJSON´®´«µ½ºǫ́ºǫ́ÈçºÎÓÃ@RequestBody½âÎöÕâЩ²»Í¬¸ñʽµÄJSON´®µÄ¡£
¶þ¡¢´úÂëչʾÐèÒªÒýÓõÄjar°ü
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 "> <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=" "> <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" value="sendAjax1"/> <br/> <input type="button" value="sendAjax2"> <br/> <input type="button" value="sendAjax3"> <br/> <input type="button" value="sendAjax4"> <br/> </body> </html>4.Ч¹ûͼ
Èý¡¢×ܽᡣ
¡¡