java循環(huán)遍歷無規(guī)則json的方式:gson、fastjson、orgjson
更新時間:2024年08月08日 09:17:53 作者:qq_41771339
這篇文章主要介紹了java循環(huán)遍歷無規(guī)則json的方式:gson、fastjson、orgjson,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
引入依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.83</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20200518</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency>
使用gson遍歷
private static testJson = "";
private static Map<String, Object> pathMap = new HashMap<>();
//通過gson遍歷所有參數(shù)
public static void getJsonMapByGson() {
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(testJson, JsonObject.class);
System.out.println(jsonObject);
analysisJson("/", jsonObject);
for (Map.Entry entry : pathMap.entrySet()) {
System.out.println(entry);
}
}
//遞歸解析Json、獲取所有鍵值對
public static void analysisJson(String pathPre, JsonObject jsonObject) {
for (String key : jsonObject.keySet()) {
JsonElement value = jsonObject.get(key);
if (value.isJsonPrimitive()) {
if (value.getAsJsonPrimitive().isString()) { //此處只取了字符串
pathMap.put(pathPre + key, value.getAsString());
}
} else if (value.isJsonObject()) {
JsonObject objectValue = value.getAsJsonObject();
analysisJson(pathPre + key, objectValue);
} else if (value.isJsonArray()) {
JsonArray jsonArray = value.getAsJsonArray();
for (int i = 0; i < jsonArray.size(); i++) {
JsonObject tempJson = new JsonObject();
tempJson.add(i + "", jsonArray.get(i));
analysisJson(pathPre + key, tempJson);
}
}
}
}使用fastjson遍歷
private static testJson = "";
private static Map<String, Object> pathMap = new HashMap<>();
//通過fastjson遍歷所有參數(shù)
public static void getJsonMapByFastjson() {
JSONObject jsonObject = JSONObject.parseObject(testJson);
System.out.println(jsonObject);
analysisJson("/", jsonObject);
for (Map.Entry entry : pathMap.entrySet()) {
System.out.println(entry);
}
}
//遞歸解析Json、獲取所有鍵值對
public static void analysisJson(String pathPre, JSONObject jsonObject) {
Set<String> set = jsonObject.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
String key = it.next().toString();
Object value = jsonObject.get(key);
if (value instanceof String || value instanceof Integer || value instanceof Float || value instanceof Double) {
pathMap.put(pathPre + key, value);
} else if (value instanceof JSONObject) {
analysisJson(pathPre + key + "/", (JSONObject) value);
} else if (value instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) value;
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject tempJson = new JSONObject();
tempJson.put(i + "", jsonArray.get(i));
analysisJson(pathPre + key + "/", tempJson);
}
}
}
}使用orgjson遍歷
private static testJson = "";
private static Map<String, Object> pathMap = new HashMap<>();
//通過org.json遍歷所有參數(shù)
public static void getJsonMapByOrgjson() {
JSONObject jsonObject = new JSONObject(testJson);
System.out.println(jsonObject);
analysisJson("/", jsonObject);
for (Map.Entry entry : pathMap.entrySet()) {
System.out.println(entry);
}
}
//遞歸解析Json、獲取所有鍵值對
public static void analysisJson(String pathPre, JSONObject jsonObject) {
Set<String> set = jsonObject.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
String key = it.next().toString();
Object value = jsonObject.get(key);
if (value instanceof String || value instanceof Integer || value instanceof Float || value instanceof Double) {
pathMap.put(pathPre + key, value);
} else if (value instanceof JSONObject) {
analysisJson(pathPre + key + "/", (JSONObject) value);
} else if (value instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) value;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject tempJson = new JSONObject();
tempJson.put(i + "", jsonArray.get(i));
analysisJson(pathPre + key + "/", tempJson);
}
}
}
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot項目讀取resources目錄下的文件的9種方式
本文主要介紹了springboot項目讀取resources目錄下的文件的9種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
springboot配置多數(shù)據(jù)源的實例(MongoDB主從)
下面小編就為大家分享一篇springboot配置多數(shù)據(jù)源的實例(MongoDB主從),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
Java long 轉(zhuǎn)成 String的實現(xiàn)
這篇文章主要介紹了Java long 轉(zhuǎn)成 String的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Netty分布式ByteBuf使用SocketChannel讀取數(shù)據(jù)過程剖析
這篇文章主要為大家介紹了Netty源碼分析ByteBuf使用SocketChannel讀取數(shù)據(jù)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03

