替换XML有效载荷或JSON有效载荷为空值。

huangapple go评论84阅读模式
英文:

Replace XML payload or json payload with empty value

问题

// 我需要在日志优化过程中在高级环境中移除不需要的日志,所以得到了移除字符串中的json或XML负载的要求。

String jsonString= "这是json字符串:{\"name\":\"xyz\",\"address\":\"pqr\"}";

String xmlString= "这是xml字符串,无法转换为XML内容的代码格式:\n\n<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();
英文:

I need to remove unwanted logs at higher environment for the logger optimization so got the requirements to remove json or XML payload from string.

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

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

&lt;customer&gt;John Smith&lt;/customer&gt;&quot;;

Tried with json using Jackson library seems working but I need to integrate in such a way that by checking condition like XML or json and then can remove payload and set empty values. Looking forward your help.

int i= str.indexOf(&quot;{&quot;);

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

ObjectNode object = (ObjectNode)jsonNode;

object.removeAll();

答案1

得分: 0

根据您的两个示例它应该如下所示

```php
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"
                )
        );
    }
}

<details>
<summary>英文:</summary>

Based on your two examples, it should look like this:

```php
static String cleanUpLogItem(String logItem) {
    return logItem.substring(0, logItem.indexOf(&quot;:&quot;)).trim();
}

Test:

public class FooBarTest {

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

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

huangapple
  • 本文由 发表于 2023年7月17日 12:13:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定