|
|
@@ -0,0 +1,40 @@
|
|
|
+package com.lc.ibps.untils;
|
|
|
+
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+public class JsonUtils {
|
|
|
+ private static final Pattern NESTED_JSON_PATTERN =
|
|
|
+ Pattern.compile("\"(\\w+)\":\"(\\{.*?\\})\"");
|
|
|
+
|
|
|
+ public static String fixNestedJson(String jsonStr) {
|
|
|
+ // 处理嵌套JSON结构
|
|
|
+ Matcher matcher = NESTED_JSON_PATTERN.matcher(jsonStr);
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ while (matcher.find()) {
|
|
|
+ matcher.appendReplacement(sb,
|
|
|
+ "\"" + matcher.group(1) + "\":" + matcher.group(2));
|
|
|
+ }
|
|
|
+ matcher.appendTail(sb);
|
|
|
+
|
|
|
+ // 处理常见格式问题
|
|
|
+ return sb.toString()
|
|
|
+ .replaceAll("\\\\\"", "\"") // 处理转义引号
|
|
|
+ .replaceAll("\"\\[", "[") // 处理数组开头
|
|
|
+ .replaceAll("\\]\"", "]") // 处理数组结尾
|
|
|
+ .replaceAll("\\\\r\\\\n", "\\\\n") // 统一换行符
|
|
|
+ .trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static JSONObject parseToObject(String jsonStr) {
|
|
|
+ return JSONUtil.parseObj(fixNestedJson(jsonStr));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static JSONArray parseToArray(String jsonStr) {
|
|
|
+ return JSONUtil.parseArray(fixNestedJson(jsonStr));
|
|
|
+ }
|
|
|
+}
|