英文:
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= "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>";
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("{");
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(":")).trim();
}
Test:
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"
)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论