package com.example.demo.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.example.demo.pojo.Student;
@RestController
public class JsonController {
@RequestMapping("/j1")
public ModelAndView j1() {
return new ModelAndView("j1.html");
}
@RequestMapping("/j2")
public String j2() {
return "hello";
}
// 返回MAP形式转换的JSON
@RequestMapping("/j3")
public Map<String, Object> j3() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("hello", 1);
map.put("world", 1.5f);
map.put("foo", true);
map.put("bar", "ok");
return map;
}
// 返回对象形式转换的JSON
@RequestMapping("/j4")
public Student j4() {
Student student = new Student();
student.setId(1);
student.setName("smith");
student.setWeight(185.5f);
student.setMale(true);
student.setLikes(Arrays.asList("football", "basketball", "volleyball"));
return student;
}
// 返回List套Map形式转换的JSON
@RequestMapping("/j5")
public List<Map<String,Object>> j5() {
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("hello", 1);
map.put("world", 1.5f);
map.put("foo", true);
map.put("bar", "yes");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("hello", 12);
map2.put("world", 12.5f);
map2.put("foo", false);
map2.put("bar", "no");
list.add(map);
list.add(map2);
return list;
}
// 返回List套对象形式转换的JSON
@RequestMapping("/j6")
public List<Student> j6() {
List<Student> students=new ArrayList<Student>();
Student student = new Student();
student.setId(1);
student.setName("smith");
student.setWeight(185.5f);
student.setMale(true);
student.setLikes(Arrays.asList("football", "basketball", "volleyball"));
Student student2 = new Student();
student2.setId(2);
student2.setName("sarah");
student2.setWeight(165.5f);
student2.setMale(false);
student2.setLikes(Arrays.asList("shopping", "market", "walk"));
students.add(student);
students.add(student2);
return students;
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 引入jquery框架 方便ajax开发 -->
<script type="text/javascript" src="js/jquery-1.8.2.min.js">
</script>
<script type="text/javascript">
//访问响应url的controller并返回三string
function ajaxPost2(url){
$.post(url,function(data){
alert(data);
});
}
//访问响应url的controller并返回MAP转换的json字符串
function ajaxPost3(url){
$.post(url,function(data){
//true参与运算相等于1
alert(data['hello']+data['world']+data['foo']+data['bar']);
//两者有区别
alert(data['hello']+" "+data['world']+" "+data['foo']+" "+data['bar']);
});
}
//访问响应url的controller并返回对象转换的json字符串
function ajaxPost4(url){
$.post(url,function(data){
//true参与运算相等于1
//alert(data['id']+data['name']+data['weights']+data['male']);
//两者有区别
alert(data['id']+" "+data['name']+" "+data['weight']+" "+data['male']);
var t="";
//普通循环
/* for(var i=0;i<data['likes'].length;i++){
t+=data['likes'][i]+" ";
}; */
//forEach循环
/* data['likes'].forEach(function(like){
t+=like+" ";
}); */
//Lambda写法
data['likes'].forEach(like=>t+=like+" ");
alert(t);
});
}
//访问响应url的controller并返回对象转换的json字符串
function ajaxPost5(url){
$.post(url,function(datas){
datas.forEach(function(data){
alert(data['hello']+" "+data['world']+" "+data['foo']+" "+data['bar']);
});
});
}
//访问响应url的controller并返回List套对象形式转换的JSON
function ajaxPost6(url){
$.post(url,function(datas){
var t="";
datas.forEach(function(data){
data['likes'].forEach(like=>t+=like+" ");
alert(data['id']+" "+data['name']+" "+data['weight']+" "+data['male']+" "+t);
t="";
});
});
}
</script>
</head>
<body>
用框架自动转换JSON
<br>
<input type="button" οnclick="ajaxPost2('j2')" value="返回字符串"/><BR>
<input type="button" οnclick="ajaxPost3('j3')" value="返回MAP转换JSON"/><BR>
<input type="button" οnclick="ajaxPost4('j4')" value="返回对象转换JSON"/><BR>
<input type="button" οnclick="ajaxPost5('j5')" value="返回List套Map形式转换的JSON"/><BR>
<input type="button" οnclick="ajaxPost6('j6')" value="返回List套对象形式转换的JSON"/><BR>
</body>
</html>