jackson 将XML payload或json payload替换为空值

wixjitnu  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(93)

我需要在更高的环境中删除不需要的日志,以便优化日志记录器,因此需要从字符串中删除json或XML有效负载。

String jsonString= "This is jsonstring:{\"name\":\"xyz",\"address\":\"pqr\"}";

String xmlString= "This is xmlString not able to convert in code format of XML content:

<customer>John Smith</customer>";

尝试使用Jackson库与JSON似乎工作,但我需要以这样一种方式集成,通过检查条件,如XML或JSON,然后可以删除负载和设置空值。期待您的帮助。

int i= str.indexOf("{");

JsonNode jsonNode = new ObjectMapper().readTree(str.substring(i));

ObjectNode object = (ObjectNode)jsonNode;

object.removeAll();
yshpjwxd

yshpjwxd1#

根据你的两个例子,它应该看起来像这样:

static String cleanUpLogItem(String logItem) {
    return logItem.substring(0, logItem.indexOf(":")).trim();
}

测试:

public class FooBarTest {

    @ParameterizedTest
    @MethodSource("dataProvider")
    void test_cleanUpLogItem(String logItem, String expected) {
        assertEquals(expected, cleanUpLogItem(logItem));
    }

    private static Stream<Arguments> dataProvider() {
        return Stream.of(
                Arguments.of(
                        "This is jsonstring:{\"name\":\"xyz\",\"address\":\"pqr\"}",
                        "This is jsonstring"
                ),
                Arguments.of(
                        "This is xmlString not able to convert in code format of XML content: <customer>John Smith</customer>",
                        "This is xmlString not able to convert in code format of XML content"
                )
    }
}

相关问题