后端ssm框架:
list转换成json字符串:
@Autowired
StudentService s;
@ResponseBody
@RequestMapping(value = "method", method = RequestMethod.POST)
public String method(Object object){
List<T> list= s.selectMethod(object);
//ObjectMapper和StringWriter都是jackson中的,通过这两个可以实现对list的序列化
ObjectMapper mapper = new ObjectMapper();
StringWriter w = new StringWriter();
//Convert between List and JSON
try {
mapper.writeValue(w, list);//开始序列化
} catch (IOException e) {
e.printStackTrace();
} finally {
System.err.println("finally");
}
System.out.println(w.toString()); //输出json格式的字符串
return w.toString();
}
前端获取方法:
$.ajax({
url:"/student/selectClassroomF",
type:"post",
data:"",
dataType:"json",
success:function (data) {
var res = "";
for (var i = 0; i < data.length; i++) {
//拼接html
res += "<h1>data[i].key</h1>"
}
$("#aim").html(res);
},
error:function () {
alert("系统错误!");
}
});