mirror of
https://gitee.com/dolphinscheduler/DolphinScheduler.git
synced 2024-12-05 05:38:30 +08:00
[DS-6606] [common] JSONUtils#getNodeString String type (#6618)
This commit is contained in:
parent
06e8e24708
commit
f60e3c219a
@ -31,6 +31,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
@ -231,7 +232,11 @@ public class JSONUtils {
|
||||
public static String getNodeString(String json, String nodeName) {
|
||||
try {
|
||||
JsonNode rootNode = objectMapper.readTree(json);
|
||||
return rootNode.has(nodeName) ? rootNode.get(nodeName).toString() : "";
|
||||
JsonNode jsonNode = rootNode.findValue(nodeName);
|
||||
if (Objects.isNull(jsonNode)) {
|
||||
return "";
|
||||
}
|
||||
return jsonNode.isTextual() ? jsonNode.asText() : jsonNode.toString();
|
||||
} catch (JsonProcessingException e) {
|
||||
return "";
|
||||
}
|
||||
|
@ -151,7 +151,9 @@ public class JSONUtilsTest {
|
||||
Assert.assertEquals("", JSONUtils.getNodeString("", "key"));
|
||||
Assert.assertEquals("", JSONUtils.getNodeString("abc", "key"));
|
||||
Assert.assertEquals("", JSONUtils.getNodeString("{\"bar\":\"foo\"}", "key"));
|
||||
Assert.assertEquals("\"foo\"", JSONUtils.getNodeString("{\"bar\":\"foo\"}", "bar"));
|
||||
Assert.assertEquals("foo", JSONUtils.getNodeString("{\"bar\":\"foo\"}", "bar"));
|
||||
Assert.assertEquals("[1,2,3]", JSONUtils.getNodeString("{\"bar\": [1,2,3]}", "bar"));
|
||||
Assert.assertEquals("{\"1\":\"2\",\"2\":3}", JSONUtils.getNodeString("{\"bar\": {\"1\":\"2\",\"2\":3}}", "bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user