mirror of
https://github.com/rememberber/MooTool.git
synced 2026-09-19 02:37:00 +08:00
json工具增加支持key value互换
This commit is contained in:
@@ -378,11 +378,11 @@
|
||||
<!-- </exclusions>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.fasterxml.jackson.core</groupId>-->
|
||||
<!-- <artifactId>jackson-databind</artifactId>-->
|
||||
<!-- <version>${jackson-databind.version}</version>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson-databind.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -12,10 +12,7 @@ import com.luoboduner.moo.tool.ui.component.FindReplaceBar;
|
||||
import com.luoboduner.moo.tool.ui.dialog.JsonResultDialog;
|
||||
import com.luoboduner.moo.tool.ui.form.MainWindow;
|
||||
import com.luoboduner.moo.tool.ui.form.func.JsonBeautyForm;
|
||||
import com.luoboduner.moo.tool.util.MockDataGenerator;
|
||||
import com.luoboduner.moo.tool.util.MybatisUtil;
|
||||
import com.luoboduner.moo.tool.util.SqliteUtil;
|
||||
import com.luoboduner.moo.tool.util.XmlReformatUtil;
|
||||
import com.luoboduner.moo.tool.util.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -551,7 +548,7 @@ public class JsonBeautyListener {
|
||||
try {
|
||||
String jsonText = jsonBeautyForm.getTextArea().getText();
|
||||
|
||||
jsonBeautyForm.getTextArea().setText(swapKeyValue(jsonText));
|
||||
jsonBeautyForm.getTextArea().setText(JsonKeyValueSwapper.swapKeysAndValues(jsonText));
|
||||
jsonBeautyForm.getTextArea().setCaretPosition(0);
|
||||
} catch (Exception e1) {
|
||||
JOptionPane.showMessageDialog(App.mainFrame, "转换失败!\n\n" + e1.getMessage(), "失败",
|
||||
@@ -701,40 +698,4 @@ public class JsonBeautyListener {
|
||||
return "未命名_" + DateFormatUtils.format(new Date(), "yyyy-MM-dd_HH-mm-ss");
|
||||
}
|
||||
|
||||
/**
|
||||
* 自己实现的json Key Value互换,原理是转成map,然后互换key和value,以及递归
|
||||
* @param jsonText
|
||||
* @return
|
||||
*/
|
||||
private static String swapKeyValue(String jsonText) {
|
||||
if (StringUtils.isBlank(jsonText)) {
|
||||
return jsonText;
|
||||
}
|
||||
if (jsonText.startsWith("{") && jsonText.endsWith("}")) {
|
||||
jsonText = jsonText.substring(1, jsonText.length() - 1);
|
||||
}
|
||||
String[] split = jsonText.split(",");
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (String s : split) {
|
||||
String[] keyValue = s.split(":");
|
||||
if (keyValue.length == 2) {
|
||||
String key = keyValue[0];
|
||||
String value = keyValue[1];
|
||||
if (key.startsWith("\"")) {
|
||||
key = key.substring(1);
|
||||
}
|
||||
if (key.endsWith("\"")) {
|
||||
key = key.substring(0, key.length() - 1);
|
||||
}
|
||||
if (value.startsWith("\"")) {
|
||||
value = value.substring(1);
|
||||
}
|
||||
if (value.endsWith("\"")) {
|
||||
value = value.substring(0, value.length() - 1);
|
||||
}
|
||||
result.append(value).append(":").append(key).append(",");
|
||||
}
|
||||
}
|
||||
return "{" + result.substring(0, result.length() - 1) + "}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.luoboduner.moo.tool.util;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
public class JsonKeyValueSwapper {
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
public static String swapKeysAndValues(String jsonString) throws JsonProcessingException {
|
||||
JsonNode rootNode = objectMapper.readTree(jsonString);
|
||||
ObjectNode swappedNode = objectMapper.createObjectNode();
|
||||
swapKeysAndValues(rootNode, swappedNode);
|
||||
return objectMapper.writeValueAsString(swappedNode);
|
||||
}
|
||||
|
||||
private static void swapKeysAndValues(JsonNode originalNode, ObjectNode swappedNode) {
|
||||
Iterator<Map.Entry<String, JsonNode>> fields = originalNode.fields();
|
||||
while (fields.hasNext()) {
|
||||
Map.Entry<String, JsonNode> field = fields.next();
|
||||
String key = field.getKey();
|
||||
JsonNode value = field.getValue();
|
||||
if (value.isObject()) {
|
||||
ObjectNode nestedSwappedNode = objectMapper.createObjectNode();
|
||||
swapKeysAndValues(value, nestedSwappedNode);
|
||||
swappedNode.set(value.asText(), nestedSwappedNode);
|
||||
} else {
|
||||
swappedNode.put(value.asText(), key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user