【问题标题】:How to filter json string and get subset json in Java?如何过滤 json 字符串并在 Java 中获取子集 json?
【发布时间】:2018-09-12 12:07:35
【问题描述】:

有没有办法过滤 JSON 字符串中的嵌套字段?

我尝试过使用 Jackson SimpleBeanPropertyFilter 进行跟踪

String jsonString = "{"
              + “\”node1\”:{“
                  + “\”field1\”:\”00\","
                  + “\”field2\”:\”test1\","
                  + “\”field3\”:\”test2\””
              + "},"
              + “\”node2\”:{“
                  + “\”field1\”:\”00\","
                  + “\”field2\”:\”test123\”,”
                  + “\”field3\”:\”test456\””
              + "}"
          + "}";

ObjectMapper mapper = new ObjectMapper()
FilterProvider filters = new SimpleFilterProvider().addFilter("filter properties by name",
                    SimpleBeanPropertyFilter.filterOutAllExcept(“field1”));
mapper.addMixIn(Object.class, PropertyFilterMixIn.class);
mapper.setFilters(filters);

它工作正常,但有没有办法在 jackson 或任何其他 Java 的 JSON 库中指定嵌套字段,如“node1.field1”?

输入

 {
  "node1": {
    "field1": "val1",
    "field2": "val2"
  },
  "node2": {
    "field1": "val1"
  }
}

过滤器

node1.field1

预期输出

{
  "node1": {
    "field1": "val1"
  }
}

【问题讨论】:

    标签: java json filter jackson


    【解决方案1】:

    我建议为此使用 JSON 路径。JSON 路径允许使用表达式过滤掉部分 JSON 树,例如:“$.node1[0].field1”。

    为了使用 JSON 路径,您需要一个底层 JSON 库(如 Jackson),然后是这个库(Maven 语法):

    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <version>2.4.0</version>
    </dependency>
    

    您可以将 Jackson 作为基础库导入到您的项目中:

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.8</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.8</version>
    </dependency>
    

    更新:

    您可以使用 JSON 路径获取带有一些附加功能的整个节点。下面是一个使用这个表达式的例子"$.node1[?(@.field1=='00')]"

    @Test
    void extractParent() {
        String jsonString = "{"
                + "\"node1\":{"
                + "\"field1\":\"00\","
                + "\"field2\":\"test1\","
                + "\"field3\":\"test2\""
                + "},"
                + "\"node2\":{"
                + "\"field1\":\"00\","
                + "\"field2\":\"test123\","
                + "\"field3\":\"test456\""
                + "}"
                + "}";
        System.out.println(jsonString);
        DocumentContext jsonPath = JsonPath.parse(jsonString);
        final JSONArray res = jsonPath.read("$.node1[?(@.field1=='00')]");
        System.out.println(res);
        Map<String, Object> composedRes = new LinkedHashMap<>();
        composedRes.put("node1", res);
        System.out.println(composedRes);
    }
    

    打印出来:

    {node1=[{"field1":"00","field2":"test1","field3":"test2"}]}
    

    这与您想要的非常接近。

    【讨论】:

    • JsonPath 将只返回值“val1”。它不会返回整个节点,我需要整个节点。
    • @GaneshKarki 请检查更新的答案是否对您有帮助。
    猜你喜欢
    • 2019-09-21
    • 2020-05-08
    • 1970-01-01
    • 2023-03-14
    • 2010-11-14
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多