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