import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JSONUtil {
/**
* 获取[]中间的字符
*/
public static Pattern pattern = Pattern.compile("\\[(.*?)]");
/**
* 获取json深度结构里面的值,如果值不存在或者路径错误,不会报错,会直接返回空
* @param jsonObject json
* @param expression json获取路径的表达式 样例:"{lll}.{mmm}.{jjjj}.[444]0.a",{}代表json对象,[]代表json数组,[]后面的数字代表取第几个数组中的元素,什么都不加代表取的属性的值
* @return
*/
public static String getString(JSONObject jsonObject, String expression) {
if (jsonObject == null) {
return null;
}
if(StrUtil.isEmpty(expression)){
return null;
}
JSONObject temp=jsonObject;
String[] array = expression.split("\\.");
for (int i = 0; i < array.length; i++) {
String s = array[i];
if (i == (array.length - 1)) {
return temp.getString(s);
}
if (s.startsWith("{") && s.endsWith("}")) {
temp = temp.getJSONObject(removeTextAndLastBracket(s));
if(temp==null){
return null;
}
}
if (s.startsWith("[") && s.contains("]")) {
String key = getParamInArray(s);
if(s.split("]").length<2){
return null;
}
int index = Integer.parseInt(s.split("]")[1]);
if(temp.getJSONArray(key)==null){
return null;
}
if(index>=temp.getJSONArray(key).size()){
return null;
}
temp = temp.getJSONArray(key).getJSONObject(index);
if(temp==null){
return null;
}
}
}
return null;
}
public static <T> T get(JSONObject jsonObject, String expression,Class<T> clazz){
String o=getString(jsonObject, expression);
if(StrUtil.isEmpty(o)){
return null;
}
return Convert.convert(clazz, o);
}
public static JSONObject getJSONObject(JSONObject jsonObject, String expression) {
if (jsonObject == null) {
return null;
}
if(StrUtil.isEmpty(expression)){
return null;
}
JSONObject temp=jsonObject;
String[] array = expression.split("\\.");
for (int i = 0; i < array.length; i++) {
String s = array[i];
if (i == (array.length - 1)) {
if(s.startsWith("{") && s.endsWith("}")){
return temp.getJSONObject(removeTextAndLastBracket(s));
}
if(s.startsWith("[") && s.contains("]")){
String key = getParamInArray(s);
if(s.split("]").length<2){
return null;
}
int index = Integer.parseInt(s.split("]")[1]);
if(temp.getJSONArray(key)==null){
return null;
}
if(index>=temp.getJSONArray(key).size()){
return null;
}
temp = temp.getJSONArray(key).getJSONObject(index);
return temp;
}
}
if (s.startsWith("{") && s.endsWith("}")) {
temp = temp.getJSONObject(removeTextAndLastBracket(s));
if(temp==null){
return null;
}
}
if (s.startsWith("[") && s.contains("]")) {
String key = getParamInArray(s);
int index = Integer.parseInt(s.split("]")[1]);
if(temp.getJSONArray(key)==null){
return null;
}
if(index>=temp.getJSONArray(key).size()){
return null;
}
temp = temp.getJSONArray(key).getJSONObject(index);
if(temp==null){
return null;
}
}
}
return null;
}
public static JSONArray getJSONArray(JSONObject jsonObject, String expression){
if (jsonObject == null) {
return null;
}
if(StrUtil.isEmpty(expression)){
return null;
}
JSONObject temp=jsonObject;
String[] array = expression.split("\\.");
for (int i = 0; i < array.length; i++) {
String s = array[i];
if (i == (array.length - 1)) {
try {
return temp.getJSONArray(removeTextAndLastBracket(s));
} catch (Exception e) {
return null;
}
}
if (s.startsWith("{") && s.endsWith("}")) {
temp = temp.getJSONObject(removeTextAndLastBracket(s));
if(temp==null){
return null;
}
}
if (s.startsWith("[") && s.contains("]")) {
String key = getParamInArray(s);
if(s.split("]").length<2){
return null;
}
int index = Integer.parseInt(s.split("]")[1]);
if(temp.getJSONArray(key)==null){
return null;
}
if(index>=temp.getJSONArray(key).size()){
return null;
}
temp = temp.getJSONArray(key).getJSONObject(index);
if(temp==null){
return null;
}
}
}
return null;
}
private static String removeTextAndLastBracket(String string) {
if (StrUtil.isEmpty(string)) {
return string;
}
StringBuilder str = new StringBuilder(string);
int i = 0;
do {
str.deleteCharAt(i);
i++;
} while (string.equals("("));
str.deleteCharAt(string.length() - 2);
return str.toString();
}
private static String getParamInArray(String s) {
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
public static void main(String[] args) {
String json="{\n" +
" \"lll\": {\n" +
" \"mmm\": {\n" +
" \"jjjj\": {\n" +
" \"444\": [\n" +
" {\n" +
" \"a\":23.24\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
JSONObject jsonObject= JSON.parseObject(json);
System.out.println(JSONUtil.get(jsonObject,"{lll}.{mmm}.{jjjj}.[444]0.a", Boolean.class));
System.out.println(JSONUtil.getString(jsonObject,"lll"));
System.out.println(JSONUtil.getJSONObject(jsonObject,"{lll}.{mmm}.{jjjj}.[444]0"));
System.out.println(JSONUtil.getJSONArray(jsonObject,"{lll}.{mmm}.{jjjj}.[444]"));
}
}
JSONUtil,兼容fast-json路径过深或错误报错的问题
于 2022-12-02 15:37:24 首次发布