詳解json在SpringBoot中的格式轉換
@RestController自動返回json
/** * json 三種實現方法 * 1 @RestController自動返回json */ @GetMapping('/json') public Student getjson() { Student student = new Student('bennyrhys',158 ); return student; }
@ResponseBody+@Controller 組合返回json
//@RestController @Controller// 類名上方 @GetMapping('/json') @ResponseBody public Student getjson() { Student student = new Student('bennyrhys',158 ); return student; }
在pom.xml 添加 阿里巴巴json jar包
//@RestController @Controller// 類名上方@GetMapping('/fastjson') public void fastjson(HttpServletResponse response){ Student student = new Student('bennyrhys',158); String data = JSON.toJSONString(student); try { sendJsonData(response, data); } catch (IOException e) { e.printStackTrace(); } } protected void sendJsonData(HttpServletResponse response, String data) throws IOException { response.setContentType('text/html;charset=UTF-8'); PrintWriter out = response.getWriter(); out.println(data); out.flush(); out.close(); }
fastjson深入理解fastJson對于json格式字符串的解析主要用到了一下三個類:
JSON:fastJson的解析器,用于JSON格式字符串與JSON對象及javaBean之間的轉換。JSONObject:fastJson提供的json對象。JSONArray:fastJson提供json數組對象。
toJSONString() 和 parseObject() 方法來將 Java 對象與 JSON 相互轉換。調用toJSONString方 法即可將對象轉換成 JSON 字符串,parseObject 方法則反過來將 JSON 字符串轉換成對象。
允許轉換預先存在的無法修改的對象(只有class、無源代碼)。Java泛型的廣泛支持。允許對象的自定義表示、允許自定義序列化類。支持任意復雜對象(具有深厚的繼承層次和廣泛使用的泛型類型)。
JSONObject 當成一個 Map<String,Object> 來看JSONArray當成一個 List 來看JSON.toString(Object)----> return StringJSON.parse(String)----->return Object
String 和 JsonObject 和 JsonArray之間轉化https://www.jb51.net/article/199602.htm
1、String轉JSONObject 或 JSONArray
JSONObject jSONObject = JSONObject.parseObject(String);JSONArray jsonArray= JSONArray.parseArray(String);
2、JSONObject中的數組提取為JSONArray提取Result對應的數組
JSONArray jsonArray= jsonObject.getJSONArray(“Result”);
3、JSONArray提取為JSONObject
JSONObject jsonObject = jsonArray.getJSONObject(0);
4、JSONObject獲取value
1、object.getString('key')
2、object.get('key')
JSONArray jsonArray= jsonObject.getJSONArray(“Result”);JSONObject jsonObject = jsonArray.getJSONObject(0);
封裝json在entiy的body返回msg
ResponseEntity可以定義返回的HttpStatus(狀態碼)和HttpHeaders(消息頭:請求頭和響應頭)
HttpStatus(狀態碼)https://blog.csdn.net/csdn1844295154/article/details/78980174
HttpHeaders(消息頭:請求頭和響應頭)https://www.cnblogs.com/honghong87/articles/6941436.html
具體查看這些內容的用法可以參考https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Disposition
ResponseEntity返回body
JsonResponseServlet
import java.io.Serializable;public class JsonResponse<T> implements Serializable { private Integer code; private String msg; private T result; public JsonResponse() { } public JsonResponse(Integer code, String msg, T result) { this.code = code; this.msg = msg; this.result = result; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getResult() { return result; } public void setResult(T result) { this.result = result; } public static class JsonResponseUtil { public static JsonResponse<Object> ok() { return ok(null); } public static <T> JsonResponse<T> ok(T result) { return new JsonResponse<>(0, '', result); } public static JsonResponse<Object> error(Integer code) { return error(code, 'error!'); } public static JsonResponse<Object> error(String msg) { return error(-1, msg); } public static JsonResponse<Object> error(Integer code, String msg) { return new JsonResponse<>(code, msg, null); } }}
controller
return ResponseEntity.ok(JsonResponse.JsonResponseUtil.ok(source));
到此這篇關于詳解json在SpringBoot中的格式轉換的文章就介紹到這了,更多相關SpringBoot json格式轉換內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: