From fed842e44203b278b933c2e2220bcd52d1766b0b Mon Sep 17 00:00:00 2001 From: Dream95 <864197662@qq.com> Date: Fri, 22 Jan 2021 18:11:38 +0800 Subject: [PATCH] =?UTF-8?q?bugfix=20=E4=BF=AE=E5=A4=8D'[]'=E6=A0=BC?= =?UTF-8?q?=E5=BC=8Fjson=E8=A7=A3=E6=9E=90=E9=94=99=E8=AF=AF=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#1242)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/metersphere/metersphere/issues/1144 Co-authored-by: zhouyx --- .../commons/utils/JsonPathUtils.java | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/backend/src/main/java/io/metersphere/commons/utils/JsonPathUtils.java b/backend/src/main/java/io/metersphere/commons/utils/JsonPathUtils.java index 239c7c09a0..296621089f 100644 --- a/backend/src/main/java/io/metersphere/commons/utils/JsonPathUtils.java +++ b/backend/src/main/java/io/metersphere/commons/utils/JsonPathUtils.java @@ -1,30 +1,23 @@ package io.metersphere.commons.utils; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONPath; import org.apache.commons.lang3.StringUtils; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class JsonPathUtils { public static List getListJson(String jsonString) { - JSONObject jsonObject = JSONObject.parseObject(jsonString); + JSON jsonObject = jsonString.startsWith("[")?JSONObject.parseArray(jsonString):JSONObject.parseObject(jsonString); List allJsons =new ArrayList<>(); // 获取到所有jsonpath后,获取所有的key - List jsonPaths = JSONPath.paths(jsonObject).keySet() - .stream() - .collect(Collectors.toList()); + List jsonPaths = new ArrayList<>(JSONPath.paths(jsonObject).keySet()); //去掉根节点key List parentNode = new ArrayList<>(); //根节点key