1、fastjson如何判断JSONObject和JSONArray,百度一下,教程还真不少,但是是阿里的fastjson的我是没有找到合适的方法。这里用一个还算可以的方法,算是实现了这个效果。
网上贴代码,有时候不把引入的包贴上去,自己使用的话还真不知道是导入那个包咧。
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>
实现代码如下所示:
例子主要实现的是解析第一层,然后解析第二层,解析第二层的时候判断是否是JSONArray还是JSONObject类型的。最后我是直接输出的,你肯定是将解析出的信息进行其他操作,这里不做叙述了。
1 package com.fline.aic.utils; 2 3 import com.alibaba.fastjson.JSONArray; 4 import com.alibaba.fastjson.JSONObject; 5 6 public class FastJosnUtils { 7 8 /** 9 * FastJSON 10 */ 11 public static void fastJson() { 12 // 1、Json字符串,JSONObject类型的 13 // String message = "{ " + " "catalogId": "IJ1009", " 14 // + " "tableName": "core_data.uc_gov_org", " + " "type": "POST", " 15 // + " "condition": "null", " + " "data": { " + " "DataSource": "'P33'", " 16 // + " "DataStamp": "'2018-08-25'", " + " "GovScCode": "'aaa'", " 17 // + " "OperEffDate": "'2018-05-02'", " + " "OrgCode": "'ww'", " 18 // + " "OrgDesc": "'ss'", " + " "OrgId": "1", " 19 // + " "OrgName": "'11111'", " + " "OrgSeq": "11", " 20 // + " "OrgShortName": "'ss'", " + " "OrgStatus": "'ss'", " 21 // + " "OrgType": "'ss'", " + " "ParentOrgId": "0", " 22 // + " "RegAddress": "'ww'", " + " "RegDate": "", " + " "RegionId": "1" " 23 // + " } " + "}"; 24 25 // 1、Json字符串,JSONArray类型的 26 String message = "{ " + " "catalogId": "IJ1009", " 27 + " "tableName": "core_data.uc_gov_org", " + " "type": "POST", " 28 + " "condition": "null", " + " "data": [{ " + " "DataSource": "'P33'", " 29 + " "DataStamp": "'2018-08-25'", " + " "GovScCode": "'aaa'", " 30 + " "OperEffDate": "'2018-05-02'", " + " "OrgCode": "'ww'", " 31 + " "OrgDesc": "'ss'", " + " "OrgId": "1", " 32 + " "OrgName": "'11111'", " + " "OrgSeq": "11", " 33 + " "OrgShortName": "'ss'", " + " "OrgStatus": "'ss'", " 34 + " "OrgType": "'ss'", " + " "ParentOrgId": "0", " 35 + " "RegAddress": "'ww'", " + " "RegDate": "", " + " "RegionId": "1" " 36 + " },{ " + 37 " "DataSource": "'P33'", " + 38 " "DataStamp": "'2018-08-25'", " + 39 " "GovScCode": "'aaa'", " + 40 " "OperEffDate": "'2018-05-02'", " + 41 " "OrgCode": "'ww'", " + 42 " "OrgDesc": "'ss'", " + 43 " "OrgId": "1", " + 44 " "OrgName": "'11111'", " + 45 " "OrgSeq": "11", " + 46 " "OrgShortName": "'ss'", " + 47 " "OrgStatus": "'ss'", " + 48 " "OrgType": "'ss'", " + 49 " "ParentOrgId": "0", " + 50 " "RegAddress": "'ww'", " + 51 " "RegDate": "", " + 52 " "RegionId": "1" " + 53 " }] " + "}"; 54 // 解析第一层{},由于多种json写到了一起,所以直接引用到了包,自行开发引用自己的Json包就行。 55 JSONObject jsonObject = JSONObject.parseObject(message); 56 String catalogId = jsonObject.getString("catalogId"); 57 String schemaTableName = jsonObject.getString("tableName"); 58 String type = jsonObject.getString("type"); 59 String condition = jsonObject.getString("condition"); 60 System.out.println("{catalogId :" + catalogId + ", schemaTableName: " + schemaTableName + ", type: " + type 61 + ", condition:" + condition + "}"); 62 63 // 解析第二层,如果根据需求可以判断是否是JSONObject还是JSONArray 64 JSONArray jsonArray = new JSONArray(); 65 JSONObject jsonData = new JSONObject(); 66 String data = jsonObject.getString("data"); 67 //百度了很对,也没有找到合适的,我是这样判断的是否是JSONArray还是JSONObject 68 if (data.contains("[") && data.contains("]")) { 69 jsonArray = JSONArray.parseArray(data); 70 System.out.println("jsonArray: " + jsonArray); 71 //然后可以解析第二层 72 for(int i=0;i< jsonArray.size();i++) { 73 JSONObject jsonArrayObject = (JSONObject) jsonArray.get(i); 74 String DataSource = jsonArrayObject.getString("DataSource"); 75 String DataStamp = jsonArrayObject.getString("DataStamp"); 76 String OrgName = jsonArrayObject.getString("OrgName"); 77 String RegAddress = jsonArrayObject.getString("RegAddress"); 78 //...等等字段 79 System.out.println("{DataSource: "+DataSource +", DataStamp: " + DataStamp + ", OrgName: " +OrgName +", RegAddress: " + RegAddress); 80 } 81 82 } else { 83 jsonData = JSONObject.parseObject(data); 84 System.out.println("jsonData: " + jsonData); 85 //然后可以解析第二层 86 String DataSource = jsonData.getString("DataSource"); 87 String DataStamp = jsonData.getString("DataStamp"); 88 String OrgName = jsonData.getString("OrgName"); 89 String RegAddress = jsonData.getString("RegAddress"); 90 //...等等字段 91 System.out.println("{DataSource: "+DataSource +", DataStamp: " + DataStamp + ", OrgName: " +OrgName +", RegAddress: " + RegAddress); 92 } 93 } 94 95 public static void main(String[] args) { 96 fastJson(); 97 98 } 99 }
2、JSON官方的判断json字符串是JSONArray还是JSONObject类型的。是这个样子搞得,百度一下,教程还是很多的。这里也简单贴一下。
1 <!-- https://mvnrepository.com/artifact/org.json/json --> 2 <dependency> 3 <groupId>org.json</groupId> 4 <artifactId>json</artifactId> 5 <version>20160810</version> 6 </dependency>
案例代码如下所示:
1 package com.fline.aic.utils; 2 3 import org.json.JSONArray; 4 import org.json.JSONObject; 5 import org.json.JSONTokener; 6 7 public class OrgJsonUtils { 8 9 /** 10 * 单层的orgJson判断是否是JSONObject还是JSONArray. 11 */ 12 public static void simpleJSONObjectOrgJson() { 13 String message = "[{ " + " "DataSource": "'P33'", " 14 + " "DataStamp": "'2018-08-25'", " + " "GovScCode": "'aaa'", " 15 + " "OperEffDate": "'2018-05-02'", " + " "OrgCode": "'ww'", " 16 + " "OrgDesc": "'ss'", " + " "OrgId": "1", " 17 + " "OrgName": "'11111'", " + " "OrgSeq": "11", " 18 + " "OrgShortName": "'ss'", " + " "OrgStatus": "'ss'", " 19 + " "OrgType": "'ss'", " + " "ParentOrgId": "0", " 20 + " "RegAddress": "'ww'", " + " "RegDate": "", " + " "RegionId": "1" " 21 + " }]"; 22 Object json = new JSONTokener(message).nextValue(); 23 if (json instanceof JSONObject) { 24 JSONObject jsonObject = (JSONObject) json; 25 System.out.println(jsonObject); 26 //自行解析即可 27 } else if (json instanceof JSONArray) { 28 JSONArray jsonArray = (JSONArray) json; 29 System.out.println(jsonArray); 30 //自行解析即可 31 } 32 } 33 34 /** 35 * 单层的orgJson判断是否是JSONObject还是JSONArray. 36 */ 37 public static void simpleJSONArrayOrgJson() { 38 String message = "{ " + " "DataSource": "'P33'", " 39 + " "DataStamp": "'2018-08-25'", " + " "GovScCode": "'aaa'", " 40 + " "OperEffDate": "'2018-05-02'", " + " "OrgCode": "'ww'", " 41 + " "OrgDesc": "'ss'", " + " "OrgId": "1", " 42 + " "OrgName": "'11111'", " + " "OrgSeq": "11", " 43 + " "OrgShortName": "'ss'", " + " "OrgStatus": "'ss'", " 44 + " "OrgType": "'ss'", " + " "ParentOrgId": "0", " 45 + " "RegAddress": "'ww'", " + " "RegDate": "", " + " "RegionId": "1" " 46 + " }"; 47 Object json = new JSONTokener(message).nextValue(); 48 if (json instanceof JSONObject) { 49 JSONObject jsonObject = (JSONObject) json; 50 System.out.println(jsonObject); 51 //自行解析即可 52 } else if (json instanceof JSONArray) { 53 JSONArray jsonArray = (JSONArray) json; 54 System.out.println(jsonArray); 55 //自行解析即可 56 } 57 } 58 59 /** 60 * JSON官方 61 */ 62 public static void doubleOrgJson() { 63 // Json字符串 64 /* 65 * String message = "{ " + " "catalogId": "IJ1009", " + 66 * " "tableName": "core_data.uc_gov_org", " + 67 * " "type": "POST", " + " "condition": "null", " + 68 * " "data": { " + " "DataSource": "'P33'", " + 69 * " "DataStamp": "'2018-08-25'", " + 70 * " "GovScCode": "'aaa'", " + 71 * " "OperEffDate": "'2018-05-02'", " + 72 * " "OrgCode": "'ww'", " + " "OrgDesc": "'ss'", " + 73 * " "OrgId": "1", " + " "OrgName": "'11111'", " + 74 * " "OrgSeq": "11", " + " "OrgShortName": "'ss'", " 75 * + " "OrgStatus": "'ss'", " + " "OrgType": "'ss'", " 76 * + " "ParentOrgId": "0", " + 77 * " "RegAddress": "'ww'", " + " "RegDate": "", " + 78 * " "RegionId": "1" " + " } " + "}"; 79 */ 80 81 String message = "{ " + " "catalogId": "IJ1009", " 82 + " "tableName": "core_data.uc_gov_org", " + " "type": "POST", " 83 + " "condition": "null", " + " "data": [{ " + " "DataSource": "'P33'", " 84 + " "DataStamp": "'2018-08-25'", " + " "GovScCode": "'aaa'", " 85 + " "OperEffDate": "'2018-05-02'", " + " "OrgCode": "'ww'", " 86 + " "OrgDesc": "'ss'", " + " "OrgId": "1", " 87 + " "OrgName": "'11111'", " + " "OrgSeq": "11", " 88 + " "OrgShortName": "'ss'", " + " "OrgStatus": "'ss'", " 89 + " "OrgType": "'ss'", " + " "ParentOrgId": "0", " 90 + " "RegAddress": "'ww'", " + " "RegDate": "", " + " "RegionId": "1" " 91 + " }] " + "}"; 92 // 解析第一层{} 93 JSONObject jsonObject = new JSONObject(message); 94 String catalogId = jsonObject.getString("catalogId"); 95 String schemaTableName = jsonObject.getString("tableName"); 96 String type = jsonObject.getString("type"); 97 String condition = jsonObject.getString("condition"); 98 System.out.println("{catalogId :" + catalogId + ", schemaTableName: " + schemaTableName + ", type: " + type 99 + ", condition:" + condition + "}"); 100 101 // 解析第二层,如果自己已经明确第二层是[]是JSONArray类型的,如下解析即可 102 JSONArray jsonArray2 = jsonObject.getJSONArray("data"); 103 // 解析第二层,如果自己已经明确第二层是{}是JSONObject类型的,如下解析即可 104 // JSONObject jsonObject2 = jsonObject.getJSONObject("data"); 105 106 107 for (int i = 0; i < jsonArray2.length(); i++) { 108 JSONObject jsonArrayObject = (JSONObject) jsonArray2.get(i); 109 String DataSource = jsonArrayObject.getString("DataSource"); 110 String DataStamp = jsonArrayObject.getString("DataStamp"); 111 String OrgName = jsonArrayObject.getString("OrgName"); 112 String RegAddress = jsonArrayObject.getString("RegAddress"); 113 // ...等等字段 114 System.out.println("{DataSource: " + DataSource + ", DataStamp: " + DataStamp + ", OrgName: " + OrgName 115 + ", RegAddress: " + RegAddress + "}"); 116 } 117 } 118 119 public static void main(String[] args) { 120 doubleOrgJson(); 121 // simpleJSONObjectOrgJson(); 122 // simpleJSONArrayOrgJson(); 123 } 124 125 }
GSON和jackson,我还未接触过,这里就不叙述了,百度也有很多教程。
待续.....