简体   繁体   English

将结果集映射到复杂的json结构

[英]map resultset into complex json structure

I am developing an application using spring boot and MySQL. 我正在使用Spring Boot和MySQL开发应用程序。 I am also using JDBCTemplate to interact with database from spring boot application. 我还使用JDBCTemplate从Spring Boot应用程序与数据库进行交互。

In the specific case, I am calling a stored procedure which returns the resultset as like below: (example data) 在特定情况下,我正在调用一个存储过程,该存储过程返回结果集,如下所示:(示例数据)

Cluster         AppType      Application
-----------------------------------------
C1               AT1            A
C1               AT1            B
C1               AT2            A
C1               AT2            B

I am mapping this result to pojo as like below: 我将这个结果映射到pojo,如下所示:

Repository: 仓库:

public List<UserSubHierarchy> getUserHierarchy() {
            List<UserSubHierarchy> listCount = new LinkedList<>();
            String sql = "   call GET_USER_SUBHIERARCHY()";

            listCount = jdbcTemplate.query(sql, new BeanPropertyRowMapper<UserSubHierarchy>(UserSubHierarchy.class));

            return listCount;
    }

Pojo: Pojo:

public class UserSubHierarchy {
    private String apm_buisness_cluster;
    private String apm_application_type;
    private String apm_application_name;
     // setter and getter
}

After this I need to process the List<UserSubHierarchy> and bring the output as follows: 在此之后,我需要处理List<UserSubHierarchy> ,并将输出如下:

[
   {
      "text":"C1",
      "children":[
         {
            "text":"AT1",
            "children":[
               {
                  "text":"A",
                  "children":null
               },
               {
                  "text":"B",
                  "children":null
               }
            ]
         },
         {
            "text":"AT2",
            "children":[
               {
                  "text":"A",
                  "children":null
               },
               {
                  "text":"B",
                  "children":null
               }
            ]
         }
      ]
   }
]

For the above JSON format I have created pojo as like below: 对于上述JSON格式,我创建了pojo,如下所示:

public class AppList{
private String text;
private List<AppList> children;
//getter and setter
}

I have tried for last two days, I couldn't bring my expected output. 我已经尝试了两天,但无法带来预期的输出。 Kindly help me or guide me to get the solution. 请帮助我或指导我获得解决方案。 I guess I am clearly mentioning all my resources, if not kindly let me know. 我想我很清楚地提到了我所有的资源,如果不能的话,请告诉我。 Thanks all in Advance. 谢谢所有提前。

For collecting the items into a map structure you can do something like this: 要将项目收集到地图结构中,您可以执行以下操作:

final Map<String, Map<String, List<UserSubHierarchy>>> out = list.stream().collect(
          Collectors.groupingBy(UserSubHierarchy::getApm_buisness_cluster,
          Collectors.groupingBy(UserSubHierarchy::getApm_application_type)));

This gives back the following data: 这将返回以下数据:

{C1={
  AT1=[
    WIMdataReader$UserSubHierarchy@7a79be86, 
    WIMdataReader$UserSubHierarchy@34ce8af7], 
  AT2=[
    WIMdataReader$UserSubHierarchy@b684286, 
    WIMdataReader$UserSubHierarchy@880ec60]}}

If your data has a fixed structure like the data above then you can skip the children item labels and just have encode the map as it is, using whatever JSON library you prefer, giving output like this: 如果您的数据像上面的数据一样具有固定的结构,那么您可以跳过子项标签,只对地图进行编码,使用您喜欢的任何JSON库,输出如下:

{  
   "C1":{  
      "AT1":[  
         "A",
         "B"
      ],
      "AT2":[  
         "A",
         "B"
      ]
   }
}

Full test code 完整的测试代码

public class WIMdataReader {
  public static class UserSubHierarchy {
    private String buisnessCluster;
    private String applicationType;
    public String getBuisnessCluster() {
      return buisnessCluster;
    }
    public String getApplicationType() {
      return applicationType;
    }
    private String applicationName;
    public UserSubHierarchy(String buisnessCluster, String applicationType, String applicationName) {
      this.buisnessCluster = buisnessCluster;
      this.applicationType = applicationType;
      this.applicationName = applicationName;
    }
    @Override
    public String toString() {
      return applicationName;
    }
  }
  private static final List<UserSubHierarchy> list = List.of(
      new UserSubHierarchy("C1", "AT1", "A"),
      new UserSubHierarchy("C1", "AT1", "B"),
      new UserSubHierarchy("C1", "AT2", "A"),
      new UserSubHierarchy("C1", "AT2", "B")
  );
  public static void main(final String... args) {
    final Map<String, Map<String, List<UserSubHierarchy>>> out =
        list.stream().collect(
            Collectors.groupingBy(UserSubHierarchy::getBuisnessCluster,
            Collectors.groupingBy(UserSubHierarchy::getApplicationType)));
    System.out.println(out);
  }
}

Output: {C1={AT1=[A, B], AT2=[A, B]}} 输出: {C1={AT1=[A, B], AT2=[A, B]}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM