From 30fba73f7287688a372627b578605b3b65650df8 Mon Sep 17 00:00:00 2001 From: zoujiangtao Date: Wed, 4 Dec 2024 17:06:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=BA=EF=BC=9A=E9=82=B9?= =?UTF-8?q?=E6=B1=9F=E6=B6=9B=20=E4=BF=AE=E6=94=B9=E5=86=85=E5=AE=B9?= =?UTF-8?q?=EF=BC=9AAPI=E6=98=A0=E5=B0=84=E5=85=AC=E5=85=B1=E5=8D=95?= =?UTF-8?q?=E6=8D=AE=20=E4=BF=AE=E6=94=B9=E6=97=B6=E9=97=B4=EF=BC=9A2024/1?= =?UTF-8?q?2/04?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sys/plugin/form/ApiMappingBillPlugin.java | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/form/ApiMappingBillPlugin.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/form/ApiMappingBillPlugin.java index cf6fd28..bf208ac 100644 --- a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/form/ApiMappingBillPlugin.java +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/plugin/form/ApiMappingBillPlugin.java @@ -205,12 +205,12 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin { } } jsonArray.add(json); - codeEdit.setText(jsonArray.toJSONString()); + codeEdit.setText(format(jsonArray.toJSONString())); logger.info("数组 → JSON:{}", jsonArray.toJSONString()); } else { JSONObject resultJson = new JSONObject(); processFloor(resultJson, "data", floors.get(0), floors, billObject); - codeEdit.setText(resultJson.toJSONString()); + codeEdit.setText(format(resultJson.toJSONString())); logger.info("对象 → JSON:{}", resultJson.toJSONString()); } } @@ -485,4 +485,45 @@ public class ApiMappingBillPlugin extends AbstractFormPlugin implements Plugin { this.getModel().setValue("shkd_defaultdata", def, dynamicObjectCollection.size() - 1); this.getModel().setValue("shkd_parentfield", pKey, dynamicObjectCollection.size() - 1); } + + public String format(String jsonString) { + StringBuilder prettyJson = new StringBuilder(); + char[] chars = jsonString.toCharArray(); + int indentLevel = 0; + + for (char ch : chars) { + switch (ch) { + case '{': + case '[': + prettyJson.append(ch); + prettyJson.append('\n'); + indentLevel++; + addIndent(prettyJson, indentLevel); + break; + case '}': + case ']': + prettyJson.append('\n'); + indentLevel--; + addIndent(prettyJson, indentLevel); + prettyJson.append(ch); + break; + case ',': + prettyJson.append(ch); + prettyJson.append('\n'); + addIndent(prettyJson, indentLevel); + break; + default: + prettyJson.append(ch); + break; + } + } + + return prettyJson.toString(); + } + + private void addIndent(StringBuilder sb, int indentLevel) { + for (int i = 0; i < indentLevel; i++) { + sb.append(" "); // 使用4个空格作为缩进 + } + } } \ No newline at end of file