maven依赖包:
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>
一、FastJson是用于java后台处理json格式数据的一个工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:
(1)速度最快,测试表明,fastjson具有极快的性能,超越任其他的java json parser。
(2)功能强大,完全支持java bean、集合、Map、日期、Enum,支持范型,支持自省。
(3)无依赖,能够直接运行在Java SE 5.0以上版本
二、FastJson对于json格式字符串的解析主要用到了一下三个类:
(1)JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换。
(2)JSONObject:fastJson提供的json对象。
(3)JSONArray:fastJson提供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 }
四、JSON格式字符串、JSON对象及JavaBean之间的相互转换
4.1) JAVA对象转JSON字符串
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 }
4.2) A. JSON字符串转JSON对象
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 }
B. 复杂JSON格式字符串与JSONObject之间的转换
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 }
4.3) A. JSON字符串转JAVA简单对象
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 }
B. JSON字符串 数组类型与JAVA对象的转换
测试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 }
C. JSON字符串 第二种方法-->数组类型与JAVA对象的转换
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 }
4.4) JAVA对象转JSON对象
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。
参考例文 --------------- >https://blog.csdn.net/xuforeverlove/article/details/80842148