java循環(huán)遍歷無(wú)規(guī)則json的方式:gson、fastjson、orgjson
更新時(shí)間:2024年08月08日 09:17:53 作者:qq_41771339
這篇文章主要介紹了java循環(huán)遍歷無(wú)規(guī)則json的方式:gson、fastjson、orgjson,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
引入依賴
<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<>();
//通過(guò)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、獲取所有鍵值對(duì)
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<>();
//通過(guò)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、獲取所有鍵值對(duì)
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<>();
//通過(guò)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、獲取所有鍵值對(duì)
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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot項(xiàng)目讀取resources目錄下的文件的9種方式
本文主要介紹了springboot項(xiàng)目讀取resources目錄下的文件的9種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
springboot配置多數(shù)據(jù)源的實(shí)例(MongoDB主從)
下面小編就為大家分享一篇springboot配置多數(shù)據(jù)源的實(shí)例(MongoDB主從),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Java long 轉(zhuǎn)成 String的實(shí)現(xiàn)
這篇文章主要介紹了Java long 轉(zhuǎn)成 String的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
java實(shí)現(xiàn)寫入并保存txt文件的代碼詳解
在本篇文章里小編給大家整理了關(guān)于java實(shí)現(xiàn)寫入并保存txt文件的代碼實(shí)例內(nèi)容,需要的朋友們可以參考學(xué)習(xí)下。2020-02-02
java 通過(guò)反射遍歷所有字段修改值的實(shí)例代碼
這篇文章主要介紹了java 通過(guò)反射遍歷所有字段修改值,通過(guò)java 的反射,遍歷所有字段,進(jìn)行一個(gè)判斷,取出來(lái)的值是帶有圖片鏈接的,進(jìn)行操作,省去了很多代碼,理解也很容易,下面跟隨小編看下實(shí)例代碼吧2021-05-05
Netty分布式ByteBuf使用SocketChannel讀取數(shù)據(jù)過(guò)程剖析
這篇文章主要為大家介紹了Netty源碼分析ByteBuf使用SocketChannel讀取數(shù)據(jù)過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03

