额外添加依赖
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
</dependency>
<!-- 添加处理json为javaBean-->
<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
JSON数据返回前台以及如何解析
JSON后台解析
1.返回POJO
@RequestMapping("/m1")
@ResponseBody //这个注解将知道现在返回的不是视图,它会将这个数据转换为JSON格式
public User m1(){
User user = new User();
user.setName("cyk");
user.setPwd("123");
return user;
}
2.返回Map
@RequestMapping("/m2")
@ResponseBody
public Map<String,Object> m2(){
Map<String, Object> map = new HashMap<>();
map.put("name","cyk");
map.put("age",12);
return map;
}
3.返回数组
@RequestMapping("/m3")
@ResponseBody
public User[] m3(){
User user = new User();
user.setName("cyk");
user.setPwd("123");
User user2 = new User();
user2.setName("cyk2");
user2.setPwd("1234");
return new User[]{user,user2};
}
4.返回List
@RequestMapping("/m4")
@ResponseBody
public List<User> m4(){
List<User> l = new ArrayList<>();
User user = new User();
user.setName("王菲");
user.setPwd("123");
User user2 = new User();
user2.setName("程萧");
user2.setPwd("1234");
l.add(user);
l.add(user2);
return l;
}
JSON前台解析
1.解析返回的POJO
<script>
$(function () {
$('#b1').click(function () {
$.ajax({
url:'${path}/json/m1',
type:'post',
success:function (data) {
alert(data.name);
alert(data.pwd);
}
})
})
})
</script>
2.解析返回的Map
$('#b2').click(function () {
$.ajax({
url:'${path}/json/m2',
type:'post',
success:function (data) {
alert(data.name);
alert(data.age);
}
})
})
3.解析返回的数组
$('#b3').click(function () {
$.ajax({
url:'${path}/json/m3',
type:'post',
success:function (data) {
for(var i = 0; i < data.length; i++){
alert(data[i].name);
alert(data[i].pwd);
}
}
})
})
4.解析返回的List
$('#b4').click(function () {
$.ajax({
url:'${path}/json/m4',
type:'post',
success:function (data) {
for(var i = 0; i < data.length; i++){
alert(data[i].name);
alert(data[i].pwd);
}
}
})
})
5.解析返回的List<Map<String,User>>
$('#b5').click(function () {
$.ajax({
url:'${path}/json/m5',
type:'post',
success:function (data) {
for(var i = 0; i < data.length; i++){
alert(data[i].u1.name);
alert(data[i].u1.pwd);
alert(data[i].u2.name);
alert(data[i].u2.pwd);
}
}
})
})
JSON数据如何使用Ajax提交到后台,后台如何解析
contentType:"application/json;charset=utf-8"
1.发送一个POJO到后台
前台
$('#b1').click(function () {
var obj = {
'name':'叶问',
'pwd':'哈哈哈'
}
$.ajax({
url:'${path}/json2/add',
type:'post',
contentType:'application/json',
data:JSON.stringify(obj),
success:function (data) {
}
})
})
后台
//前台如何提交一个User对象
@RequestMapping("/add")
//User user入参只能提供表单数据
public String add(@RequestBody User user){
System.out.println(user.getName()+user.getPwd());
return "msg";
}
记得一定要添加@RequestBody,否则无法解析
2.发送一组POJO到后台
前台
$('#b2').click(function () {
var obj = {
'name':'叶问',
'pwd':'哈哈哈'
};
var obj2 = {
'name':'小叶问',
'pwd':'哈哈哈hhhh'
};
var arr = new Array();
arr.push(obj);
arr.push(obj2);
$.ajax({
url:'${path}/json2/addList',
type:'post',
contentType:'application/json',
data:JSON.stringify(arr),
success:function (data) {
if(data.code === 2000)
alert("ok");
}
})
})
后台
@RequestMapping("/addList")
@ResponseBody
public Map<String,Integer> addList(@RequestBody List<User> list){
Map<String,Integer> map = new HashMap<>();
System.out.println(list);
map.put("code",2000);
return map;
}
关于Form提交数据与Ajax自定义JSON数据提交的区别
1.form提交方式请求截图
2.通过ajax组装的json数据发送分析
二者发送组装数据的区域不同,所以处理方式也不同
对于form表单提交数据它的contentType是属于Content-Type:application/x-www-form-urlencoded
对于ajax发送json则是 Content-Type:application/json
XML数据交互
对于很多第三方开发,还是有很多会采用XML作为数据交互,比如微信
1.添加额外依赖
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.8</version>
</dependency>
2.方法返回数据类型定义
@Controller
@RequestMapping("/xml")
public class XMLController {
@RequestMapping(value = "/m1", produces = {MediaType.APPLICATION_XML_VALUE})
@ResponseBody
public User m1(){
//将数据转换为xml形式user
User u = new User();
u.setName("小明");
u.setPwd("哈哈哈");
return u;
}
}
3.结果