如何使用 sql 或嵌套动态条件过滤 java 列表中的数据列表

How to filter list of data in list in java with sql or nested dynamic condition

如何使用 java 动态过滤数据,假设我们有数据(地图列表/json 数组,没有 pojo 映射)。

[
  {
    "id": "1001",
    "type": "Internal",
    "status": "Closed"
  },
  {
    "id": "1002",
    "type": "External",
    "status": "Closed"
  },
  {
    "id": "1003",
    "type": "Internal",
    "status": "Open"
  },
  {
    "id": "1004",
    "type": "Internal",
    "status": "Open"
  }
]

现在我们需要将过滤后的数据输出为id > 1001 and ( type: 'External' or status: 'Open" )

 [ 
  {
    "id": "1002",
    "type": "External",
    "status": "Closed"
  },
  {
    "id": "1003",
    "type": "Internal",
    "status": "Open"
  },
  {
    "id": "1004",
    "type": "Internal",
    "status": "Open"
  } 
]

关于如何实现这一目标的任何建议?

您可以通过以下方法实现此目的:-

  1. 将json转换为字符串
  2. 构造JSON数组>> JSON数组json数组=新JSON数组(converted_json_String)
  3. 从JsonArray中取出jsonArrayObject并构造一个列表
  4. 使用Collections.sort方法并在JSON键上部署你的比较逻辑 并构建你的排序 json.

试试这个代码,我假设你有一个对象列表。

List<YourCLass> filteredList = list
                    .stream()
                    .filter(obj -> obj.getId() > 1001 && (obj.getStatus().equals("Open") || obj.getType().equals("External")))
                    .collect(Collectors.toList());

filteredList 具有您期望的对象列表,使用对象映射器您可以将其作为 json 格式获取。

String filteredObjJson = new ObjectMapper().writeValuesAsString(filteredList);

在 pom.xml 中添加此依赖项或下载库并将其添加到您的库中

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
</dependency>

使用 JSON 路径,下面是我作为示例发布的几个 JSONPath 查询

P.S :- Ur "Id" 应为 Integer 类型以进行操作 > 或 <

代码:-

public static void main(String[] args) {
    String jsonData = "[\r\n" + 
            "  {\r\n" + 
            "    \"id\": 1001,\r\n" + 
            "    \"type\": \"Internal\",\r\n" + 
            "    \"status\": \"Closed\"\r\n" + 
            "  },\r\n" + 
            "  {\r\n" + 
            "    \"id\": 1002,\r\n" + 
            "    \"type\": \"External\",\r\n" + 
            "    \"status\": \"Closed\"\r\n" + 
            "  },\r\n" + 
            "  {\r\n" + 
            "    \"id\": 1003,\r\n" + 
            "    \"type\": \"Internal\",\r\n" + 
            "    \"status\": \"Open\"\r\n" + 
            "  },\r\n" + 
            "  {\r\n" + 
            "    \"id\": 1004,\r\n" + 
            "    \"type\": \"Internal\",\r\n" + 
            "    \"status\": \"Open\"\r\n" + 
            "  }\r\n" + 
            "]";
    
    String filterId = "$.[?(@.id > 1001)]"; //For Id > 1001
    String filterType =  "$.[?(@.type in ['External'])]"; //for External type
    String filterTypeAndId =  "$.[?((@.id > 1001) && (@.type in ['Internal']))]"; //for External type with Id > 1001
    String filterTypeAndId2 =  "$.[?((@.id > 1001) && (@.type in ['Internal', 'External']))]"; //for External type with Id > 1001
    DocumentContext documentContext = JsonPath.parse(jsonData);
    System.out.println(documentContext.read(filterId).toString());
    System.out.println(documentContext.read(filterType).toString());
    System.out.println(documentContext.read(filterTypeAndId).toString());
    System.out.println(documentContext.read(filterTypeAndId2).toString());
}

结果是:-

[{"id":1002,"type":"External","status":"Closed"},{"id":1003,"type":"Internal","status":"Open"},{"id":1004,"type":"Internal","status":"Open"}]

[{"id":1002,"type":"External","status":"Closed"}]

[{"id":1003,"type":"Internal","status":"Open"},{"id":1004,"type":"Internal","status":"Open"}]

[{"id":1002,"type":"External","status":"Closed"},{"id":1003,"type":"Internal","status":"Open"},{"id":1004,"type":"Internal","status":"Open"}]

我们可以使用javascript能力来解决条件。我们可以使用 Java ScriptEngineManager 来 运行 javascript 表达式: 表达式可以是 type = 'Internal' AND status ='Open'

    private String getResult(String expression) {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        try {
            return engine.eval(expression).toString();
        } catch (Exception e) {
        }
        return null;
    }