【问题标题】:How to populate list and piece them together in POJO class如何在 POJO 类中填充列表并将它们拼凑在一起
【发布时间】:2021-10-10 02:08:08
【问题描述】:

我有这个 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;
}
}

-----------------------------------DistributionGroup.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。 我在填充/设置列表值并将它们拼凑在一起并将 Group 作为列表返回时遇到了困难。

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

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;
        
    }

感谢任何帮助。

【问题讨论】:

    标签: java json arraylist pojo


    【解决方案1】:

    您的模型或多或少代表了您的 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") 添加到您的类的 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));
    

    【讨论】:

    • 谢谢。这有帮助。
    猜你喜欢
    • 2022-09-23
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 2023-03-06
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多