首页 前端知识 java循环遍历无规则json的三种方式:gson、fastjson、orgjson

java循环遍历无规则json的三种方式:gson、fastjson、orgjson

2024-05-12 17:05:58 前端知识 前端哥 328 811 我要收藏

引入依赖

<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遍历所有参数
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遍历所有参数
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遍历所有参数
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);
			}
		}
	}
}

转载请注明出处或者链接地址:https://www.qianduange.cn//article/8442.html
标签
评论
会员中心 联系我 留言建议 回顶部
复制成功!