Spring生成Json通常有以下三种形式:
1、第一种方式是spring2时代的产物,也就是每个json视图controller配置一个Jsoniew。
如:<bean id="defaultJsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
或者<bean id="defaultJsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
同样要用jackson的jar包。
2、第二种使用JSON工具将对象序列化成json,常用工具Jackson,fastjson,gson。
利用HttpServletResponse,然后获取response.getOutputStream()或response.getWriter()
直接输出。
例如:
public class JsonUtil
{
private static Gson gson=new Gson();
/**
* @MethodName : toJson
* @Description : 将对象转为JSON串,此方法能够满足大部分需求
* @param src
* :将要被转化的对象
* @return :转化后的JSON串
*/
public static String toJson(Object src) {
if (src == null) {
return gson.toJson(JsonNull.INSTANCE);
}
return gson.toJson(src);
}
}
或者用:String jsondata = JSONArray.fromObject(list).toString();//不能用map转map转json会带[]格式为:[{},{}]
//list转json格式{a:b,c:d} easyui只能识别外层为 { } 的json
3、第三种利用spring mvc3的注解@ResponseBody
然后使用spring mvc的默认配置就可以返回json了,不过需要jackson的jar包哦。这种方式最常用。
注意:当springMVC-servlet.xml中使用<mvc:annotation-driven/>时,如果是3.1之前已经默认注入AnnotationMethodHandlerAdapter,3.1之后默认注入RequestMappingHandlerAdapter只需加上上面提及的jar包即可!
例如:
@RequestMapping(value = "/advert/getStudentByIris", method = RequestMethod.POST)
@ResponseBody
public ResponseJSONObject getStudentByIris(Model model, @Param("Iris") String Iris, HttpServletResponse response) {
AStudent student = studentService.getStuByIris(Iris);
if (student != null)
return new ResponseJSONObject(true, student);
else
return new ResponseJSONObject(false, "查无此人");
}
对应的ResponseJSONObject类为:(这里的类是随意的,也可以Teacher类,Person类等等。。)
/**
* Common reture response json object for callback.
* @author 英招
*/
public class ResponseJSONObject {
/**
* Determine whether request is ok/error
*/
private boolean result;
/**
* Redirect url if need to jump
*/
private String redirectUrl;
/**
* If result=false, given the error message; If result=true, it will be null
*/
private String message;
/**
* To some insert operation, the insert new id will be return in this field.
*/
private int id = 1;
/**
* Used for complex return object, wrapper this in final json
*/
private Object returnObject;
public ResponseJSONObject() {
}
public ResponseJSONObject(boolean result) {
this.result = result;
}
public ResponseJSONObject(boolean result, Object returnObject) {
this.result = result;
this.returnObject = returnObject;
}
public ResponseJSONObject(boolean result, String message){
this.result = result;
this.message = message;
}
public ResponseJSONObject(boolean result, String message, int id) {
this.result = result;
this.message = message;
this.id = id;
}
public ResponseJSONObject(boolean result, String message, int id, Object returnObject) {
this.result = result;
this.message = message;
this.id = id;
this.returnObject = returnObject;
}
public boolean isResult() {
return result;
}
public void setResult(boolean result) {
this.result = result;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Object getReturnObject() {
return returnObject;
}
public void setReturnObject(Object returnObject) {
this.returnObject = returnObject;
}
public String getRedirectUrl() {
return redirectUrl;
}
public void setRedirectUrl(String redirectUrl) {
this.redirectUrl = redirectUrl;
}
}
对应输出的Json为:
{
"result": true,
"redirectUrl": null,
"message": null,
"id": 1,
"returnObject": {
"id": 7,
"name": "佛斯特",
"studentId": "1214010340",
"gradeNum": "小学四年级",
"classNum": "五班",
"photo": "15",
"iris": "1"
}
}
Json解析需要创建一个与Json中的属性相对应的解析类,而且属性名也必须相同!
类如需要解析这样一个Json:
{
"result": true,
"redirectUrl": null,
"message": null,
"id": 1,
"returnObject": {
"id": 7,
"name": "佛斯特",
"studentId": "1214010340",
"gradeNum": "小学四年级",
"classNum": "五班",
"photo": "15",
"iris": "1"
}
}
解析就需要这样几个类:
一、JsonObject类:(类名可以随意取,不一定要与后台服务器保持一致)
package com.advert.bean; /** * Created by 英招 on 2018/3/19. */ public class ReturnObject { int id; String name; String studentId; String gradeNum; String classNum; String photo; String Iris; public ReturnObject(int id, String name, String studentId, String gradeNum, String classNum, String photo, String iris) { this.id = id; this.name = name; this.studentId = studentId; this.gradeNum = gradeNum; this.classNum = classNum; this.photo = photo; Iris = iris; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getStudentId() { return studentId; } public void setStudentId(String studentId) { this.studentId = studentId; } public String getGradeNum() { return gradeNum; } public void setGradeNum(String gradeNum) { this.gradeNum = gradeNum; } public String getClassNum() { return classNum; } public void setClassNum(String classNum) { this.classNum = classNum; } public String getPhoto() { return photo; } public void setPhoto(String photo) { this.photo = photo; } public String getIris() { return Iris; } public void setIris(String iris) { Iris = iris; } }
二、ReturnObject类,这个类名也是随意取,但是它在JsonObject中的变量名必须为 returnObject,要与Json串保持一致!!
例如:如果类名为Teacher,那么在JsonObject中就必须写成: Teacher returnObject;
代码如下:
package com.advert.bean; /** * Created by 英招 on 2018/1/30. */ public class JsonObject { boolean result; String redirectUrl; String message; int id; ReturnObject returnObject; public JsonObject(boolean result, String redirectUrl, String message, int id, ReturnObject returnObject) { this.result = result; this.redirectUrl = redirectUrl; this.message = message; this.id = id; this.returnObject = returnObject; } public JsonObject() { } public boolean isResult() { return result; } public void setResult(boolean result) { this.result = result; } public String getRedirectUrl() { return redirectUrl; } public void setRedirectUrl(String redirectUrl) { this.redirectUrl = redirectUrl; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public int getId() { return id; } public void setId(int id) { this.id = id; } public ReturnObject getReturnObject() { return returnObject; } public void setReturnObject(ReturnObject returnObject) { this.returnObject = returnObject; } } 代码如下:(这里假设returnObject中存储的是Teacher类型的数据) Gson gson = new Gson(); try{ JSONObject jsonObject = new JSONObject(response); JSONObject jsonObject1 = jsonObject.getJSONObject("returnObject"); TagCategoryList tcl = gson.fromJson(jsonObject1.toString(), new TypeToken<Teacher>() { }.getType()); List<CookBook> list = tcl.getData(); listener.onSuccess(list); } catch(JSONException e){ e.printStackTrace(); }