简体   繁体   English

嵌套 Json Object 转换为 POJO 时返回 Null

[英]Nested Json Object Returns Null When Converting to POJO

I have the below JSON that I'm trying to read into a JSON node and then map to a Java POJO object.我有以下 JSON,我正在尝试将其读入 JSON 节点,然后将 map 读入 Java POJO object。

{
  "operation": "myoperation",
  "input": {
    "environment": "myEnv",
    "stage": "beta"
  }
}

Below is the POJO class下面是POJO class

@JsonIgnoreProperties(ignoreUnknown = true)
public class Input {
    private String environment;
    private String stage;

    public String getEnvironment() {
        return apolloEnvironment;
    }

    public String getStage() {
        return stage;
    }
}

When I read the above tree using the mapping below, I get null for the environment and stage properties.当我使用下面的映射读取上面的树时,我得到null的环境和阶段属性。

Input myInput = OBJECT_MAPPER.treeToValue(inputJson, Input.class);
System.out.println(myInput.getEnvironment()); //returns null
System.out.println(myInput.getStage()); //returns null

I would like to read the environment and stage property values, how can I do this with the above logic?我想读取环境和舞台属性值,如何使用上述逻辑执行此操作?

The Input object in your JSON is wrapped by another object. So instead of trying to parse the whole Tree as Input , you need to obtain the Value mapped to the property input and then parse it. JSON 中的Input object 被另一个 object 包装。因此,与其尝试将整个树解析为Input ,不如获取映射到属性input的值,然后解析它。

Note that setters should be in place in your Input class.请注意,setter 应该在您的Input class 中就位。

String jsonString = """
    {
      "operation": "myoperation",
      "input": {
        "environment": "myEnv",
        "stage": "beta"
      }
    }
    """;

ObjectMapper mapper = new ObjectMapper();        
JsonNode node = mapper.readTree(jsonString).get("input");
    
Input myInput = mapper.treeToValue(node, Input.class);
System.out.println(myInput.getEnvironment());
System.out.println(myInput.getStage());

Output: Output:

myEnv
beta

Another option would be to declare another class, wrapping Input , let's say InputWrapper .另一种选择是声明另一个 class,包装Input ,比方说InputWrapper The would allow to deserialize the JSON you directly into the InputWrapper instance and then access nested Input via getter method.这将允许将 JSON 直接反序列化到InputWrapper实例中,然后通过 getter 方法访问嵌套的Input

Here's how such wrapper class might look like (Lombok's annotations used for brevity):下面是这样的包装器 class 的样子(为简洁起见,使用了 Lombok 的注释):

@Setter
@Getter
public static class InputWrapper {
    private String operation;
    private Input input;
}

@Setter
@Getter
public static class Input {
    private String environment;
    private String stage;
}

Parsing logic:解析逻辑:

String jsonString = """
    {
      "operation": "myoperation",
      "input": {
        "environment": "myEnv",
        "stage": "beta"
      }
    }
    """;

ObjectMapper mapper = new ObjectMapper();        

InputWrapper inputWrapper = mapper.readValue(jsonString, InputWrapper.class);
Input myInput = inputWrapper.getInput();
        
System.out.println(myInput.getEnvironment());
System.out.println(myInput.getStage());

Output: Output:

myEnv
beta

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

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