com.github.cosycode.ext.se.util.GsonUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of extend-mod Show documentation
Show all versions of extend-mod Show documentation
扩展模块, 用于存放一些非常用的工具或模块的扩展类, 例如在poi基础上扩展的excel的导入模块, 模拟按键模块
The newest version!
package com.github.cosycode.ext.se.util;
import com.github.cosycode.common.util.otr.PrintTool;
import com.google.gson.JsonElement;
import lombok.NonNull;
/**
* Description :
*
* created in 2022/12/8
*
*
* @author CPF
* @since 0.2.2
**/
public class GsonUtils {
public static JsonElement getJsonElement(JsonElement jsonElement, @NonNull String expression) {
String[] split = expression.split("\\.");
for (String s : split) {
if (s.startsWith("[]")) {
int idx = Integer.parseInt(s.substring(2));
jsonElement = jsonElement.getAsJsonArray().get(idx);
} else if (s.startsWith("{}")) {
String substring = s.substring(2);
jsonElement = jsonElement.getAsJsonObject().get(substring);
} else {
throw new IllegalArgumentException(PrintTool.format("not support the expression: {}, subExpression: {}, json:{}", s, expression, jsonElement));
}
}
return jsonElement;
}
}