简体   繁体   English

从子对象映射json字段

[英]Map json field from child object

Here is the description of issue to convert Json string to json. 以下是将Json字符串转换为json的问题说明。

Address{
    private long id;
    private String city;
}

Employee{
    private long id;
    private String name;
    private Address address;
}

Client will send a request with JSON of employee 客户将使用员工的JSON发送请求

{"id": 1, "name": "ABC", "address": {"id": 1, "city": "XYZ"}}

Now i want to convert the input json into below format where addressId = id field of Address class. 现在我想将输入json转换为以下格式,其中addressId = Address类的id字段。

Output{
    private long id;
    private String name;
    private long addressId; 
}

is there any way to achieve this. 有没有办法实现这一目标。 I have tried Jackson and Gson also. 我也尝试过Jackson和Gson。

With Jackson, you can add a class AddressId to wrap the address with just the id field and an annotated constructor that takes an AddressId as argument. 使用Jackson,您可以添加一个class AddressId来包装地址,只包含id字段和带有AddressId作为参数的带注释的构造函数。 Add getters and other constructors as needed. 根据需要添加getter和其他构造函数。 This doesn't require defining Employee and Address classes only Output . 这不需要仅定义EmployeeAddressOutput

class Output {
    private long id;
    private String name;
    private long addressId;

    @JsonCreator
    public Output(
            @JsonProperty("id") long id,
            @JsonProperty("name") String name,
            @JsonProperty("address") AddressId address) {
        this.id = id;
        this.name = name;
        this.addressId = address.getId();
    }
}

class AddressId {
    private long id;
}

In this case, you would also need to configure your ObjectMapper to quietly ignore unknown JSON fields: 在这种情况下,您还需要配置ObjectMapper以安静地忽略未知的JSON字段:

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Another alternative, that doesn't require class AddressId , would be to create a custom setter for the addressId field based on the Map<String, Object> that Jackson uses internally when deserializing. 另一个不需要class AddressId替代方法是根据杰克逊在反序列化时内部使用的Map<String, Object>addressId字段创建自定义setter。 This is how Output would look like in this case (add other setters/getters you may need): 这就是Output在这种情况下的样子(添加你可能需要的其他setter / getters):

class Output {
    private long id;
    private String name;

    @JsonProperty("address")
    public void setAddressId(final Map<String, Object> address) {
        addressId = (Integer) address.get("id");
    }

    private long addressId;
}

You can use Gson api to convert object from json. 您可以使用Gson api从json转换对象。

Similar to the below code. 与下面的代码类似。 Not sure whether it has no syntax error. 不确定它是否没有语法错误。 But, this is possible. 但是,这是可能的。

Gson gson = new Gson();
Employee emp = gson.fromJson("{"id": 1, "name": "ABC", "address": {"id": 1, "city": "XYZ"}}", 
    Employee.class);

If the above code doesn't work out, try using dozer 如果上述代码无效,请尝试使用dozer

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

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