如何在 POJO class 中填充列表并将它们拼凑在一起

How to populate list and piece them together in POJO class

我有这个 JSON,我将其转换为由 3 个不同的 类 组成的 POJO。

{
    "Id": "2a0dd1fc",
    "name": "AABBCCDD",
    "description": "test",
    "active": true,
    "Groups": [
      {
        "agentGroups": [
          {
            "Id": "AXSNqSWSILMPnVvB-Cdc"
          }
        ],
        "order": 1,
        "duration": 0
      },
      {
        "agentGroups": [
          {
            "Id": "AXZlGzTR4pYEiRgUOqOL"
          }
        ],
        "order": 2,
        "duration": 60
      }
    ]
  }

------------------------------------AgentGroup.java----- ------------------------------

public class AgentGroup{

       public String id;
//Getter & Setters
}

---------------------------------Group.java----- ------------------------------

public class Group{
   ** public List<AgentGroup> agentGroups;**
    public int order;
    public int duration;
//Getter & Setters
public void setAgentGroups(List<AgentGroup> agentGroups) {
this.agentGroups = agentGroups;
}
}

--------------------------------分布Group.java---- ------------------------------

public class DistributionGroup{
    public String id;
    public String name;
    public String description;
    public boolean active;
    **public List<Group> groups;**
//Getter & Setters
public void setGroups(List<Group> groups) {
this.groups = groups;
}
}

可以看到Group是一个List,agentGroup也是一个List。 我很难 populating/setting 列出列表值并将它们拼凑起来,return 组作为列表。

我试过这样设置,但似乎不起作用: //函数调用

List<Group> groups=createDistributionGroup(teamIds,1)

函数:

public static Group createDistributionGroup(List<String> teamIds, int order) {
        Group grp= new Group();
        grp.setOrder(order);
        grp.setDuration(60);

        //Now How do I put AgentGroup in a list and return the group.       
        
        
        return grp;
        
    }

感谢任何帮助。

您的模型或多或少代表了您的 json。这是一个简单的测试,您可以用来验证它。

String s = "{\n"
    + "    \"id\": \"2a0dd1fc\",\n"
    + "    \"name\": \"AABBCCDD\",\n"
    + "    \"description\": \"test\",\n"
    + "    \"active\": true,\n"
    + "    \"groups\": [\n"
    + "      {\n"
    + "        \"agentGroups\": [\n"
    + "          {\n"
    + "            \"id\": \"AXSNqSWSILMPnVvB-Cdc\"\n"
    + "          }\n"
    + "        ],\n"
    + "        \"order\": 1,\n"
    + "        \"duration\": 0\n"
    + "      },\n"
    + "      {\n"
    + "        \"agentGroups\": [\n"
    + "          {\n"
    + "            \"id\": \"AXZlGzTR4pYEiRgUOqOL\"\n"
    + "          }\n"
    + "        ],\n"
    + "        \"order\": 2,\n"
    + "        \"duration\": 60\n"
    + "      }\n"
    + "    ]\n"
    + "  }";

DistributionGroup distributionGroup = new ObjectMapper().reader()
    .readValue(s, DistributionGroup.class);

我确实对您的 json 做了一些更改,我将字段 Id 设置为 id,并将 Groups 设置为 groups,请注意大小写差异。如果您不能更改 json,那么您必须向模型属性添加注释,让它们知道它们不符合 bean 规范。例如,如果您希望在您的 json 中保留 Id,则将 @JsonAlias("Id") 添加到您的 class 中的 ID 属性,因此该字段看起来像

@JsonAlias("Id")
public String id;

要在 createDistributionGroup 方法中将 AgentGroups 添加到您的组,它看起来像这样:

AgentGroup ag1 = new AgentGroup();
ag1.setId("10");

AgentGroup ag2 = new AgentGroup();
ag1.setId("20");

grp.setAgentGroups(Arrays.asList(ag1, ag2));