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 classOrgJsonUtils {8
9 /**10 * 单层的orgJson判断是否是JSONObject还是JSONArray.11 */
12 public static voidsimpleJSONObjectOrgJson() {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 = newJSONTokener(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 voidsimpleJSONArrayOrgJson() {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 = newJSONTokener(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 voiddoubleOrgJson() {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 = newJSONObject(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:" +type99 + ", 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:" +OrgName115 + ", RegAddress:" + RegAddress + "}");116 }117 }118
119 public static voidmain(String[] args) {120 doubleOrgJson();121 //simpleJSONObjectOrgJson();122 //simpleJSONArrayOrgJson();
123 }124
125 }