java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java循环遍历无规则json方式

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

作者:qq_41771339

这篇文章主要介绍了java循环遍历无规则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遍历所有参数
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);
			}
		}
	}
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文