1、fastjson如何判断JSONObject和JSONArray,百度一下,教程还真不少,但是是阿里的fastjson的我是没有找到合适的方法。这里用一个还算可以的方法,算是实现了这个效果。
网上贴代码,有时候不把引入的包贴上去,自己使用的话还真不知道是导入那个包咧。
maven依赖的如下所示:
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.</version>
</dependency>
实现代码如下所示:
例子主要实现的是解析第一层,然后解析第二层,解析第二层的时候判断是否是JSONArray还是JSONObject类型的。最后我是直接输出的,你肯定是将解析出的信息进行其他操作,这里不做叙述了。
package com.fline.aic.utils; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; public class FastJosnUtils { /**
* FastJSON
*/
public static void fastJson() {
// 1、Json字符串,JSONObject类型的
// String message = "{\r\n" + " \"catalogId\": \"IJ1009\",\r\n"
// + " \"tableName\": \"core_data.uc_gov_org\",\r\n" + " \"type\": \"POST\",\r\n"
// + " \"condition\": \"null\",\r\n" + " \"data\": {\r\n" + " \"DataSource\": \"'P33'\",\r\n"
// + " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
// + " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
// + " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
// + " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
// + " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
// + " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
// + " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
// + " }\r\n" + "}"; // 1、Json字符串,JSONArray类型的
String message = "{\r\n" + " \"catalogId\": \"IJ1009\",\r\n"
+ " \"tableName\": \"core_data.uc_gov_org\",\r\n" + " \"type\": \"POST\",\r\n"
+ " \"condition\": \"null\",\r\n" + " \"data\": [{\r\n" + " \"DataSource\": \"'P33'\",\r\n"
+ " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
+ " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
+ " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
+ " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
+ " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
+ " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
+ " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
+ " },{\r\n" +
" \"DataSource\": \"'P33'\",\r\n" +
" \"DataStamp\": \"'2018-08-25'\",\r\n" +
" \"GovScCode\": \"'aaa'\",\r\n" +
" \"OperEffDate\": \"'2018-05-02'\",\r\n" +
" \"OrgCode\": \"'ww'\",\r\n" +
" \"OrgDesc\": \"'ss'\",\r\n" +
" \"OrgId\": \"1\",\r\n" +
" \"OrgName\": \"'11111'\",\r\n" +
" \"OrgSeq\": \"11\",\r\n" +
" \"OrgShortName\": \"'ss'\",\r\n" +
" \"OrgStatus\": \"'ss'\",\r\n" +
" \"OrgType\": \"'ss'\",\r\n" +
" \"ParentOrgId\": \"0\",\r\n" +
" \"RegAddress\": \"'ww'\",\r\n" +
" \"RegDate\": \"\",\r\n" +
" \"RegionId\": \"1\"\r\n" +
" }]\r\n" + "}";
// 解析第一层{},由于多种json写到了一起,所以直接引用到了包,自行开发引用自己的Json包就行。
JSONObject jsonObject = JSONObject.parseObject(message);
String catalogId = jsonObject.getString("catalogId");
String schemaTableName = jsonObject.getString("tableName");
String type = jsonObject.getString("type");
String condition = jsonObject.getString("condition");
System.out.println("{catalogId :" + catalogId + ", schemaTableName: " + schemaTableName + ", type: " + type
+ ", condition:" + condition + "}"); // 解析第二层,如果根据需求可以判断是否是JSONObject还是JSONArray
JSONArray jsonArray = new JSONArray();
JSONObject jsonData = new JSONObject();
String data = jsonObject.getString("data");
//百度了很对,也没有找到合适的,我是这样判断的是否是JSONArray还是JSONObject
if (data.contains("[") && data.contains("]")) {
jsonArray = JSONArray.parseArray(data);
System.out.println("jsonArray: " + jsonArray);
//然后可以解析第二层
for(int i=;i< jsonArray.size();i++) {
JSONObject jsonArrayObject = (JSONObject) jsonArray.get(i);
String DataSource = jsonArrayObject.getString("DataSource");
String DataStamp = jsonArrayObject.getString("DataStamp");
String OrgName = jsonArrayObject.getString("OrgName");
String RegAddress = jsonArrayObject.getString("RegAddress");
//...等等字段
System.out.println("{DataSource: "+DataSource +", DataStamp: " + DataStamp + ", OrgName: " +OrgName +", RegAddress: " + RegAddress);
} } else {
jsonData = JSONObject.parseObject(data);
System.out.println("jsonData: " + jsonData);
//然后可以解析第二层
String DataSource = jsonData.getString("DataSource");
String DataStamp = jsonData.getString("DataStamp");
String OrgName = jsonData.getString("OrgName");
String RegAddress = jsonData.getString("RegAddress");
//...等等字段
System.out.println("{DataSource: "+DataSource +", DataStamp: " + DataStamp + ", OrgName: " +OrgName +", RegAddress: " + RegAddress);
}
} public static void main(String[] args) {
fastJson(); }
}
2、JSON官方的判断json字符串是JSONArray还是JSONObject类型的。是这个样子搞得,百度一下,教程还是很多的。这里也简单贴一下。
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version></version>
</dependency>
案例代码如下所示:
package com.fline.aic.utils; import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener; public class OrgJsonUtils { /**
* 单层的orgJson判断是否是JSONObject还是JSONArray.
*/
public static void simpleJSONObjectOrgJson() {
String message = "[{\r\n" + " \"DataSource\": \"'P33'\",\r\n"
+ " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
+ " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
+ " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
+ " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
+ " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
+ " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
+ " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
+ " }]";
Object json = new JSONTokener(message).nextValue();
if (json instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) json;
System.out.println(jsonObject);
//自行解析即可
} else if (json instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) json;
System.out.println(jsonArray);
//自行解析即可
}
} /**
* 单层的orgJson判断是否是JSONObject还是JSONArray.
*/
public static void simpleJSONArrayOrgJson() {
String message = "{\r\n" + " \"DataSource\": \"'P33'\",\r\n"
+ " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
+ " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
+ " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
+ " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
+ " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
+ " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
+ " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
+ " }";
Object json = new JSONTokener(message).nextValue();
if (json instanceof JSONObject) {
JSONObject jsonObject = (JSONObject) json;
System.out.println(jsonObject);
//自行解析即可
} else if (json instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) json;
System.out.println(jsonArray);
//自行解析即可
}
} /**
* JSON官方
*/
public static void doubleOrgJson() {
// Json字符串
/*
* String message = "{\r\n" + " \"catalogId\": \"IJ1009\",\r\n" +
* " \"tableName\": \"core_data.uc_gov_org\",\r\n" +
* " \"type\": \"POST\",\r\n" + " \"condition\": \"null\",\r\n" +
* " \"data\": {\r\n" + " \"DataSource\": \"'P33'\",\r\n" +
* " \"DataStamp\": \"'2018-08-25'\",\r\n" +
* " \"GovScCode\": \"'aaa'\",\r\n" +
* " \"OperEffDate\": \"'2018-05-02'\",\r\n" +
* " \"OrgCode\": \"'ww'\",\r\n" + " \"OrgDesc\": \"'ss'\",\r\n" +
* " \"OrgId\": \"1\",\r\n" + " \"OrgName\": \"'11111'\",\r\n" +
* " \"OrgSeq\": \"11\",\r\n" + " \"OrgShortName\": \"'ss'\",\r\n"
* + " \"OrgStatus\": \"'ss'\",\r\n" + " \"OrgType\": \"'ss'\",\r\n"
* + " \"ParentOrgId\": \"0\",\r\n" +
* " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" +
* " \"RegionId\": \"1\"\r\n" + " }\r\n" + "}";
*/ String message = "{\r\n" + " \"catalogId\": \"IJ1009\",\r\n"
+ " \"tableName\": \"core_data.uc_gov_org\",\r\n" + " \"type\": \"POST\",\r\n"
+ " \"condition\": \"null\",\r\n" + " \"data\": [{\r\n" + " \"DataSource\": \"'P33'\",\r\n"
+ " \"DataStamp\": \"'2018-08-25'\",\r\n" + " \"GovScCode\": \"'aaa'\",\r\n"
+ " \"OperEffDate\": \"'2018-05-02'\",\r\n" + " \"OrgCode\": \"'ww'\",\r\n"
+ " \"OrgDesc\": \"'ss'\",\r\n" + " \"OrgId\": \"1\",\r\n"
+ " \"OrgName\": \"'11111'\",\r\n" + " \"OrgSeq\": \"11\",\r\n"
+ " \"OrgShortName\": \"'ss'\",\r\n" + " \"OrgStatus\": \"'ss'\",\r\n"
+ " \"OrgType\": \"'ss'\",\r\n" + " \"ParentOrgId\": \"0\",\r\n"
+ " \"RegAddress\": \"'ww'\",\r\n" + " \"RegDate\": \"\",\r\n" + " \"RegionId\": \"1\"\r\n"
+ " }]\r\n" + "}";
// 解析第一层{}
JSONObject jsonObject = new JSONObject(message);
String catalogId = jsonObject.getString("catalogId");
String schemaTableName = jsonObject.getString("tableName");
String type = jsonObject.getString("type");
String condition = jsonObject.getString("condition");
System.out.println("{catalogId :" + catalogId + ", schemaTableName: " + schemaTableName + ", type: " + type
+ ", condition:" + condition + "}"); // 解析第二层,如果自己已经明确第二层是[]是JSONArray类型的,如下解析即可
JSONArray jsonArray2 = jsonObject.getJSONArray("data");
// 解析第二层,如果自己已经明确第二层是{}是JSONObject类型的,如下解析即可
// JSONObject jsonObject2 = jsonObject.getJSONObject("data"); for (int i = ; i < jsonArray2.length(); i++) {
JSONObject jsonArrayObject = (JSONObject) jsonArray2.get(i);
String DataSource = jsonArrayObject.getString("DataSource");
String DataStamp = jsonArrayObject.getString("DataStamp");
String OrgName = jsonArrayObject.getString("OrgName");
String RegAddress = jsonArrayObject.getString("RegAddress");
// ...等等字段
System.out.println("{DataSource: " + DataSource + ", DataStamp: " + DataStamp + ", OrgName: " + OrgName
+ ", RegAddress: " + RegAddress + "}");
}
} public static void main(String[] args) {
doubleOrgJson();
// simpleJSONObjectOrgJson();
// simpleJSONArrayOrgJson();
} }
GSON和jackson,我还未接触过,这里就不叙述了,百度也有很多教程。
待续.....