1 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> 2 <dependency> 3 <groupId>com.alibaba</groupId> 4 <artifactId>fastjson</artifactId> 5 <version>1.2.47</version> 6 </dependency>
(1)速度最快,測試代表,fastjson具備極快的性能,超越任其餘的java json parser。
(2)功能強大,徹底支持java bean、集合、Map、日期、Enum,支持範型,支持自省。
(3)無依賴,可以直接運行在Java SE 5.0以上版本java
(1)JSON:fastJson的解析器,用於JSON格式字符串與JSON對象及javaBean之間的轉換。
(2)JSONObject:fastJson提供的json對象。
(3)JSONArray:fastJson提供json數組對象。json
1 package com.xxx.controller; 2 3 import java.io.Serializable; 4 public class Data implements Serializable { 5 private static final long serialVersionUID = -6957361951748382519L; 6 private String id; 7 private String suborderNo; 8 private String organUnitType; 9 private String action; 10 private String parent; 11 private String organUnitFullName; 12 private Long ordinal; 13 get、set方法省略。。。 14 }
1 package com.xxx.controller; 2 3 import java.io.Serializable; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 public class Error implements Serializable { 8 9 private static final long serialVersionUID = -432908543160176349L; 10 11 private String code; 12 private String message; 13 private String success; 14 private List<Data> data = new ArrayList<>(); 15 get、set方法省略。。。 16 }
1 //java對象轉json字符串 2 public static void beanTojson() { 3 Data data = new Data(); 4 data.setAction("add"); 5 data.setId("1"); 6 data.setOrdinal(8L); 7 data.setOrganUnitFullName("testJSON"); 8 data.setParent("0"); 9 data.setSuborderNo("58961"); 10 11 String s = JSON.toJSONString(data); 12 System.out.println("toJsonString()方法:s=" + s); 13 //輸出結果{"action":"add","id":"1","ordinal":8,"organUnitFullName":"testJSON","parent":"0","suborderNo":"58961"} 14 }
1 //json字符串轉json對象 2 public static void jsonToJsonBean() { 3 String s ="{\"action\":\"add\",\"id\":\"1\",\"ordinal\":8,\"organUnitFullName\":\"testJSON\",\"parent\":\"0\",\"suborderNo\":\"58961\"}"; 4 5 JSONObject jsonObject = JSON.parseObject(s); 6 String action = jsonObject.getString("action"); 7 String id = jsonObject.getString("id"); 8 System.out.println("action ="+action);//add 9 System.out.println("id ="+id);//1 10 System.out.println("jsonObject ="+jsonObject); 11 //action =add 12 //id =1 13 //jsonObject ={"parent":"0","organUnitFullName":"testJSON","action":"add","id":"1","suborderNo":"58961","ordinal":8} 14 }
1 public static void jsonToBean() { 2 String str ="{\"meta\":{\"code\":\"0\",\"message\":\"同步成功!\"},\"data\":{\"orderno\":\"U_2018062790915774\",\"suborderno\":\"SUB_2018062797348039\",\"type\":\"organunit\",\"result\":{\"organunit\":{\"totalCount\":2,\"successCount\":0,\"failCount\":2,\"errors\":[{\"code\":\"UUM70004\",\"message\":\"組織單元名稱不能爲空\",\"data\":{\"id\":\"254\",\"suborderNo\":\"SUB_2018062797348039\",\"organUnitType\":\"部門\",\"action\":\"add\",\"parent\":\"10000\",\"ordinal\":0,\"organUnitFullName\":\"組織單元全稱\"},\"success\":false},{\"code\":\"UUM70004\",\"message\":\"組織單元名稱不能爲空\",\"data\":{\"id\":\"255\",\"suborderNo\":\"SUB_2018062797348039\",\"organUnitType\":\"部門\",\"action\":\"add\",\"parent\":\"10000\",\"ordinal\":0,\"organUnitFullName\":\"組織單元全稱\"},\"success\":false}]},\"role\":{\"totalCount\":0,\"successCount\":0,\"failCount\":0,\"errors\":[]},\"user\":{\"totalCount\":0,\"successCount\":0,\"failCount\":0,\"errors\":[]}}}}"; 3 JSONObject jsonObject = JSON.parseObject(str); 4 JSONObject data = jsonObject.getJSONObject("data"); 5 JSONObject result = data.getJSONObject("result"); 6 7 String organunit1 = result.getString("organunit"); 8 System.out.println(organunit1); 9 JSONObject organunit = result.getJSONObject("organunit"); 10 11 JSONArray errors2 = organunit.getJSONArray("errors"); 12 13 List<Error> error = JSON.parseObject(errors2.toJSONString(), new TypeReference<List<Error>>() { 14 }); 15 }
1 //json字符串轉java簡單對象 2 public static void jsonStrToJavaBean() { 3 String s ="{\"action\":\"add\",\"id\":\"1\",\"ordinal\":8,\"organUnitFullName\":\"testJSON\",\"parent\":\"0\",\"suborderNo\":\"58961\"}"; 4 Data data = JSON.parseObject(s, Data.class); 5 System.out.println("data對象"+data.toString()); 6 System.out.println("action="+data.getAction()+"---id="+data.getId()); 7 //data對象Data{id='1', suborderNo='58961', organUnitType='null', action='add', parent='0', organUnitFullName='testJSON', ordinal=8} 8 //action=add---id=1 9 10 /** 11 * 另外一種方式轉對象 12 */ 13 Data dd = JSON.parseObject(s, new TypeReference<Data>() {}); 14 System.out.println("另外一種方式獲取data對象"+dd.toString()); 15 System.out.println("另外一種方式獲取="+dd.getAction()+"---id="+dd.getId()); 16 //另外一種方式獲取data對象Data{id='1', suborderNo='58961', organUnitType='null', action='add', parent='0', organUnitFullName='testJSON', ordinal=8} 17 //另外一種方式獲取=add---id=1 18 }
測試json字符串數組
1 {"errors":[{"code":"UUM70004","message":"組織單元名稱不能爲空","data":{"id":"254","suborderNo":"SUB_2018062797348039","organUnitType":"部門","action":"add","parent":"10000","ordinal":0,"organUnitFullName":"組織單元全稱"},"success":false},{"code":"UUM70004","message":"組織單元名稱不能爲空","data":{"id":"255","suborderNo":"SUB_2018062797348039","organUnitType":"部門","action":"add","parent":"10000","ordinal":0,"organUnitFullName":"組織單元全稱"},"success":false}]}
1 //json字符串--數組型與JSONArray對象之間的轉換 2 public static void jsonStrToJSONArray() { 3 String str = "{\"errors\":[{\"code\":\"UUM70004\",\"message\":\"組織單元名稱不能爲空\",\"data\":{\"id\":\"254\",\"suborderNo\":\"SUB_2018062797348039\",\"organUnitType\":\"部門\",\"action\":\"add\",\"parent\":\"10000\",\"ordinal\":0,\"organUnitFullName\":\"組織單元全稱\"},\"success\":false},{\"code\":\"UUM70004\",\"message\":\"組織單元名稱不能爲空\",\"data\":{\"id\":\"255\",\"suborderNo\":\"SUB_2018062797348039\",\"organUnitType\":\"部門\",\"action\":\"add\",\"parent\":\"10000\",\"ordinal\":0,\"organUnitFullName\":\"組織單元全稱\"},\"success\":false}]}"; 4 JSONObject jsonObject = JSON.parseObject(str); 5 JSONArray error = jsonObject.getJSONArray("errors"); 6 List<Error> errors = JSON.parseObject(error.toJSONString(), new TypeReference<List<Error>>() { 7 }); 8 for (Error e: errors) { 9 //Error的屬性 10 System.out.println("Error屬性="+e.getSuccess()); 11 System.out.println("Error屬性="+e.getCode()); 12 System.out.println("Error屬性="+e.getMessage()); 13 //Error集合屬性 14 List<Data> datas = e.getData(); 15 for (Data d: datas) { 16 System.out.println("data對象屬性="+d.getId()); 17 System.out.println("data對象屬性="+d.getAction()); 18 System.out.println("data對象屬性="+d.getSuborderNo()); 19 } 20 } 21 //Error屬性=false 22 //Error屬性=UUM70004 23 //Error屬性=組織單元名稱不能爲空 24 //data對象屬性=254 25 //data對象屬性=add 26 //data對象屬性=SUB_2018062797348039 27 28 //Error屬性=false 29 //Error屬性=UUM70004 30 //Error屬性=組織單元名稱不能爲空 31 //data對象屬性=255 32 //data對象屬性=add 33 //data對象屬性=SUB_2018062797348039 34 }
1 //第二種方法:json字符串--數組型與JSONArray對象之間的轉換 2 @Test 3 public void jsonStrToJSONArray2() { 4 String str = "{\"errors\":[{\"code\":\"UUM70004\",\"message\":\"組織單元名稱不能爲空\",\"data\":{\"id\":\"254\",\"suborderNo\":\"SUB_2018062797348039\",\"organUnitType\":\"部門\",\"action\":\"add\",\"parent\":\"10000\",\"ordinal\":0,\"organUnitFullName\":\"組織單元全稱\"},\"success\":false},{\"code\":\"UUM70004\",\"message\":\"組織單元名稱不能爲空\",\"data\":{\"id\":\"255\",\"suborderNo\":\"SUB_2018062797348039\",\"organUnitType\":\"部門\",\"action\":\"add\",\"parent\":\"10000\",\"ordinal\":0,\"organUnitFullName\":\"組織單元全稱\"},\"success\":false}]}"; 5 //獲取jsonobject對象 6 JSONObject jsonObject = JSON.parseObject(str); 7 //把對象轉換成jsonArray數組 8 JSONArray error = jsonObject.getJSONArray("errors"); 9 //error==>[{"code":"UUM70004","message":"組織單元名稱不能爲空","data":{"id":"254","suborderNo":"SUB_2018062797348039","organUnitType":"部門","action":"add","parent":"10000","ordinal":0,"organUnitFullName":"組織單元全稱"},"success":false},{"code":"UUM70004","message":"組織單元名稱不能爲空","data":{"id":"255","suborderNo":"SUB_2018062797348039","organUnitType":"部門","action":"add","parent":"10000","ordinal":0,"organUnitFullName":"組織單元全稱"},"success":false}] 10 //將數組轉換成字符串 11 String jsonString = JSONObject.toJSONString(error);//將array數組轉換成字符串 12 //將字符串轉成list集合 13 List<Error> errors = JSONObject.parseArray(jsonString, Error.class);//把字符串轉換成集合 14 for (Error e: errors) { 15 //Error的屬性 16 System.out.println("另外一種數組轉換Error屬性="+e.getSuccess()); 17 System.out.println("另外一種數組轉換Error屬性="+e.getCode()); 18 System.out.println("另外一種數組轉換Error屬性="+e.getMessage()); 19 //Error集合屬性 20 List<Data> datas = e.getData(); 21 for (Data d: datas) { 22 System.out.println("另外一種數組轉換data對象屬性="+d.getId()); 23 System.out.println("另外一種數組轉換data對象屬性="+d.getAction()); 24 System.out.println("另外一種數組轉換data對象屬性="+d.getSuborderNo()); 25 } 26 } 27 //另外一種數組轉換Error屬性=false 28 //另外一種數組轉換Error屬性=UUM70004 29 //另外一種數組轉換Error屬性=組織單元名稱不能爲空 30 //另外一種數組轉換data對象屬性=254 31 //另外一種數組轉換data對象屬性=add 32 //另外一種數組轉換data對象屬性=SUB_2018062797348039 33 34 //另外一種數組轉換Error屬性=false 35 //另外一種數組轉換Error屬性=UUM70004 36 //另外一種數組轉換Error屬性=組織單元名稱不能爲空 37 //另外一種數組轉換data對象屬性=255 38 //另外一種數組轉換data對象屬性=add 39 //另外一種數組轉換data對象屬性=SUB_2018062797348039 40 }
1 //javabean轉json對象 2 public static void jsonBenToJsonObject() { 3 Data data = new Data(); 4 data.setAction("add"); 5 data.setId("1"); 6 data.setOrdinal(8L); 7 data.setOrganUnitFullName("testJSON"); 8 data.setParent("0"); 9 data.setSuborderNo("58961"); 10 JSONObject jsonObj = (JSONObject) JSON.toJSON(data); 11 JSON json = (JSON) JSON.toJSON(data); 12 System.out.println("jsonObj"+jsonObj); 13 System.out.println("json對象"+json); 14 //jsonObj{"parent":"0","organUnitFullName":"testJSON","action":"add","id":"1","suborderNo":"58961","ordinal":8} 15 //json對象{"parent":"0","organUnitFullName":"testJSON","action":"add","id":"1","suborderNo":"58961","ordinal":8} 16 }
(1)對於JSON對象與JSON格式字符串的轉換能夠直接用 toJSONString()這個方法。
(2)javaBean與JSON格式字符串之間的轉換要用到:JSON.toJSONString(obj);
(3)javaBean與json對象間的轉換使用:JSON.toJSON(obj),而後使用強制類型轉換,JSONObject或者JSONArray。
maven
參考例文 --------------- >http://www.javashuo.com/article/p-ezafmmbw-ga.html工具