fastjson如何判斷JSONObject和JSONArray

一、fastjson如何判斷JSONObject和JSONArray,百度一下,教程還真很多,可是是阿里的fastjson的我是沒有找到合適的方法。這裏用一個還算能夠的方法,算是實現了這個效果。json

網上貼代碼,有時候不把引入的包貼上去,本身使用的話還真不知道是導入那個包咧。maven

maven依賴的以下所示:spa

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>

 實現代碼以下所示:code

例子主要實現的是解析第一層,而後解析第二層,解析第二層的時候判斷是不是JSONArray仍是JSONObject類型的。最後我是直接輸出的,你確定是將解析出的信息進行其餘操做,這裏不作敘述了。blog

 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         // 一、Json字符串,JSONObject類型的
13 //        String message = "{\r\n" + "    \"catalogId\": \"IJ1009\",\r\n"
14 //                + "    \"tableName\": \"core_data.uc_gov_org\",\r\n" + "    \"type\": \"POST\",\r\n"
15 //                + "    \"condition\": \"null\",\r\n" + "    \"data\": {\r\n" + "        \"DataSource\": \"'P33'\",\r\n"
16 //                + "        \"DataStamp\": \"'2018-08-25'\",\r\n" + "        \"GovScCode\": \"'aaa'\",\r\n"
17 //                + "        \"OperEffDate\": \"'2018-05-02'\",\r\n" + "        \"OrgCode\": \"'ww'\",\r\n"
18 //                + "        \"OrgDesc\": \"'ss'\",\r\n" + "        \"OrgId\": \"1\",\r\n"
19 //                + "        \"OrgName\": \"'11111'\",\r\n" + "        \"OrgSeq\": \"11\",\r\n"
20 //                + "        \"OrgShortName\": \"'ss'\",\r\n" + "        \"OrgStatus\": \"'ss'\",\r\n"
21 //                + "        \"OrgType\": \"'ss'\",\r\n" + "        \"ParentOrgId\": \"0\",\r\n"
22 //                + "        \"RegAddress\": \"'ww'\",\r\n" + "        \"RegDate\": \"\",\r\n" + "        \"RegionId\": \"1\"\r\n"
23 //                + "    }\r\n" + "}";
24         
25         // 一、Json字符串,JSONArray類型的
26         String message = "{\r\n" + "    \"catalogId\": \"IJ1009\",\r\n"
27                 + "    \"tableName\": \"core_data.uc_gov_org\",\r\n" + "    \"type\": \"POST\",\r\n"
28                 + "    \"condition\": \"null\",\r\n" + "    \"data\": [{\r\n" + "        \"DataSource\": \"'P33'\",\r\n"
29                 + "        \"DataStamp\": \"'2018-08-25'\",\r\n" + "        \"GovScCode\": \"'aaa'\",\r\n"
30                 + "        \"OperEffDate\": \"'2018-05-02'\",\r\n" + "        \"OrgCode\": \"'ww'\",\r\n"
31                 + "        \"OrgDesc\": \"'ss'\",\r\n" + "        \"OrgId\": \"1\",\r\n"
32                 + "        \"OrgName\": \"'11111'\",\r\n" + "        \"OrgSeq\": \"11\",\r\n"
33                 + "        \"OrgShortName\": \"'ss'\",\r\n" + "        \"OrgStatus\": \"'ss'\",\r\n"
34                 + "        \"OrgType\": \"'ss'\",\r\n" + "        \"ParentOrgId\": \"0\",\r\n"
35                 + "        \"RegAddress\": \"'ww'\",\r\n" + "        \"RegDate\": \"\",\r\n" + "        \"RegionId\": \"1\"\r\n"
36                 + "    },{\r\n" + 
37                 "        \"DataSource\": \"'P33'\",\r\n" + 
38                 "        \"DataStamp\": \"'2018-08-25'\",\r\n" + 
39                 "        \"GovScCode\": \"'aaa'\",\r\n" + 
40                 "        \"OperEffDate\": \"'2018-05-02'\",\r\n" + 
41                 "        \"OrgCode\": \"'ww'\",\r\n" + 
42                 "        \"OrgDesc\": \"'ss'\",\r\n" + 
43                 "        \"OrgId\": \"1\",\r\n" + 
44                 "        \"OrgName\": \"'11111'\",\r\n" + 
45                 "        \"OrgSeq\": \"11\",\r\n" + 
46                 "        \"OrgShortName\": \"'ss'\",\r\n" + 
47                 "        \"OrgStatus\": \"'ss'\",\r\n" + 
48                 "        \"OrgType\": \"'ss'\",\r\n" + 
49                 "        \"ParentOrgId\": \"0\",\r\n" + 
50                 "        \"RegAddress\": \"'ww'\",\r\n" + 
51                 "        \"RegDate\": \"\",\r\n" + 
52                 "        \"RegionId\": \"1\"\r\n" + 
53                 "    }]\r\n" + "}";
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 }

 二、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 = "[{\r\n" + "        \"DataSource\": \"'P33'\",\r\n"
 14                 + "        \"DataStamp\": \"'2018-08-25'\",\r\n" + "        \"GovScCode\": \"'aaa'\",\r\n"
 15                 + "        \"OperEffDate\": \"'2018-05-02'\",\r\n" + "        \"OrgCode\": \"'ww'\",\r\n"
 16                 + "        \"OrgDesc\": \"'ss'\",\r\n" + "        \"OrgId\": \"1\",\r\n"
 17                 + "        \"OrgName\": \"'11111'\",\r\n" + "        \"OrgSeq\": \"11\",\r\n"
 18                 + "        \"OrgShortName\": \"'ss'\",\r\n" + "        \"OrgStatus\": \"'ss'\",\r\n"
 19                 + "        \"OrgType\": \"'ss'\",\r\n" + "        \"ParentOrgId\": \"0\",\r\n"
 20                 + "        \"RegAddress\": \"'ww'\",\r\n" + "        \"RegDate\": \"\",\r\n" + "        \"RegionId\": \"1\"\r\n"
 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 = "{\r\n" + "        \"DataSource\": \"'P33'\",\r\n"
 39                 + "        \"DataStamp\": \"'2018-08-25'\",\r\n" + "        \"GovScCode\": \"'aaa'\",\r\n"
 40                 + "        \"OperEffDate\": \"'2018-05-02'\",\r\n" + "        \"OrgCode\": \"'ww'\",\r\n"
 41                 + "        \"OrgDesc\": \"'ss'\",\r\n" + "        \"OrgId\": \"1\",\r\n"
 42                 + "        \"OrgName\": \"'11111'\",\r\n" + "        \"OrgSeq\": \"11\",\r\n"
 43                 + "        \"OrgShortName\": \"'ss'\",\r\n" + "        \"OrgStatus\": \"'ss'\",\r\n"
 44                 + "        \"OrgType\": \"'ss'\",\r\n" + "        \"ParentOrgId\": \"0\",\r\n"
 45                 + "        \"RegAddress\": \"'ww'\",\r\n" + "        \"RegDate\": \"\",\r\n" + "        \"RegionId\": \"1\"\r\n"
 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 = "{\r\n" + "    \"catalogId\": \"IJ1009\",\r\n" +
 66          * "    \"tableName\": \"core_data.uc_gov_org\",\r\n" +
 67          * "    \"type\": \"POST\",\r\n" + "    \"condition\": \"null\",\r\n" +
 68          * "    \"data\": {\r\n" + "        \"DataSource\": \"'P33'\",\r\n" +
 69          * "        \"DataStamp\": \"'2018-08-25'\",\r\n" +
 70          * "        \"GovScCode\": \"'aaa'\",\r\n" +
 71          * "        \"OperEffDate\": \"'2018-05-02'\",\r\n" +
 72          * "        \"OrgCode\": \"'ww'\",\r\n" + "        \"OrgDesc\": \"'ss'\",\r\n" +
 73          * "        \"OrgId\": \"1\",\r\n" + "        \"OrgName\": \"'11111'\",\r\n" +
 74          * "        \"OrgSeq\": \"11\",\r\n" + "        \"OrgShortName\": \"'ss'\",\r\n"
 75          * + "        \"OrgStatus\": \"'ss'\",\r\n" + "        \"OrgType\": \"'ss'\",\r\n"
 76          * + "        \"ParentOrgId\": \"0\",\r\n" +
 77          * "        \"RegAddress\": \"'ww'\",\r\n" + "        \"RegDate\": \"\",\r\n" +
 78          * "        \"RegionId\": \"1\"\r\n" + "    }\r\n" + "}";
 79          */
 80 
 81         String message = "{\r\n" + "    \"catalogId\": \"IJ1009\",\r\n"
 82                 + "    \"tableName\": \"core_data.uc_gov_org\",\r\n" + "    \"type\": \"POST\",\r\n"
 83                 + "    \"condition\": \"null\",\r\n" + "    \"data\": [{\r\n" + "        \"DataSource\": \"'P33'\",\r\n"
 84                 + "        \"DataStamp\": \"'2018-08-25'\",\r\n" + "        \"GovScCode\": \"'aaa'\",\r\n"
 85                 + "        \"OperEffDate\": \"'2018-05-02'\",\r\n" + "        \"OrgCode\": \"'ww'\",\r\n"
 86                 + "        \"OrgDesc\": \"'ss'\",\r\n" + "        \"OrgId\": \"1\",\r\n"
 87                 + "        \"OrgName\": \"'11111'\",\r\n" + "        \"OrgSeq\": \"11\",\r\n"
 88                 + "        \"OrgShortName\": \"'ss'\",\r\n" + "        \"OrgStatus\": \"'ss'\",\r\n"
 89                 + "        \"OrgType\": \"'ss'\",\r\n" + "        \"ParentOrgId\": \"0\",\r\n"
 90                 + "        \"RegAddress\": \"'ww'\",\r\n" + "        \"RegDate\": \"\",\r\n" + "        \"RegionId\": \"1\"\r\n"
 91                 + "    }]\r\n" + "}";
 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,我還未接觸過,這裏就不敘述了,百度也有不少教程。字符串

待續.....get

相關文章
相關標籤/搜索