实现将在服务器获取客户端传来的JSON字符串,并把JSON字符串转成JSON对象并取得其中属性值
方式1:解析JSON字符串使用json.jar来实现,需要新增jar如下
首先在web中配置spring 编码过滤器,防止乱码
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
sendAjax.jsp中利用JSON.stringify()把一个JS对象实例[object:object]转化为json格式的字符串。发送一个getJSONString.spring请求,后面加时间是为了保证浏览器每次都把它当做一个不同的请求,不调用浏览器缓存。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'sendAjax.jsp' starting page</title>
<script src="jquery-1.3.2.js"></script>
<script src="json2.js"></script>
<script type="text/javascript">
function userinfo(username,password){
this.username = username;
this.password = password;
}
function sendAjax(){
var userinfoRef = new userinfo("自来也","123");
//JSON.stringify() 方法用于将 JavaScript 值转换为 JSON 字符串
var jsonStringRef = JSON.stringify(userinfoRef);
//加上时间函数就能保证你每次得到的不是浏览器的缓存
$.post("getJSONString.spring?t="+new Date().getTime(),{
jsonString : jsonStringRef
});
}
</script>
</head>
<body>
<input type="submit" onclick="sendAjax()" value="send">
</body>
</html>
控制层将JSON字符串转换成对象,取得其中的值
package controller;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class GetJSONString {
@RequestMapping(value = "getJSONString")
public String getJSONString(@RequestParam("jsonString") String jsonString) {
JSONObject object = JSONObject.fromObject(jsonString);
System.out.println(object.get("username"));
System.out.println(object.get("password"));
return "test.jsp";
}
}
方式2:解析字符串使用springMVC4提供的一种从JSON字符串自动转换成实体的技术,只需要新增jjackson-all-1.9.8.jar
新建实体类生成get和set方法
private String username;
private String password;
控制层方法中使用@RequestBody注解,在前台只需要向控制层提交一段JSON格式requestbody体,spring4MVC就会自动将其拼装成javaBean。createJSON()方法需要调用一个无参的构造函数来实例化一个Userinto实体,进而调用name和password属性对应的方法
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import entity.Userinfo;
@Controller
public class GetJSONStringToObject {
@RequestMapping(value = "createJSONObjectURL", method = RequestMethod.POST)
public String createJSON(@RequestBody Userinfo userinfo) {
System.out.println("username value=" + userinfo.getUsername());
System.out.println("password value=" + userinfo.getPassword());
return "test.jsp";
}
}
在springMVC-servlet中添加<mvc:annotation-driven />注解使JSON字符串自动转换成实体类
<?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="controller" />
<mvc:annotation-driven />
</beans>
sendAjax.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 sendAjax(){
var userinfoRef = new userinfo('今天很热', '123');
var jsonStringRef = JSON.stringify(userinfoRef);
$.ajax({
type: "POST",
data: jsonStringRef,
url: "createJSONObjectURL.spring?t=" + new Date().getTime(),
contentType: "application/json"
});
}
</script>
</head>
<body>
<input type="button" onclick="sendAjax()"/>
</body>
</html>