本文介绍了杰克逊:将json的某些字段映射到类的内部字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将json的某些字段映射到类的内部字段。例如

I want to map some fields of json to inner fields of a class. e.g

{
 values:[{
 "name":"Abc",
 "age":18,
 "street":"test",
 "postalcoad":"1231412"
},
 {
  "name":"ccvb",
 "age":20,
 "street":"test2",
 "postalcoad":"123"
  }
]}

关注我的java课程

@JsonIgnoreProperties(ignoreUnknown = true)
public class Customer{
   @JsonProperty("name")
   private string name;

   @JsonProperty("age")
   private int age;

   private Address address;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Address{
   @JsonProperty("street")
   private string street;

   @JsonProperty("postalcode")
   private string postalcode;
}


ObjectMapper mapper = new ObjectMapper();
Customer[] c = mapper.readValue(mapper.readTree(json).get("values").toString(), Customer[].class);

它返回没有地址的Customer对象。知道如何从这个json创建Address对象。

It returns me Customer object without Address. Any idea how can i create Address object from this json.

推荐答案

其中一个选项是使用 @JsonCreator 注释:

    @JsonCreator
    public Customer(
            @JsonProperty("name") String name,
            @JsonProperty("age")  int age,
            @JsonProperty("street") String street,
            @JsonProperty("postalcode")   String postalcode
    ) {
        this.name = name;
        this.age = age;
        this.address = new Address();
        this.address.street = street;
        this.address.postalcode = postalcode;
    }

第二个选项是创建自定义反序列化器并绑定你的使用 @JsonDeserialize 注释使用反序列化程序的类

Second option is create custom deserializer and bind your class with deserializer using @JsonDeserialize annotation

@JsonDeserialize(using = CustomerDeserializer.class)
public static class Customer{
  ....
}

public class CustomerDeserializer extends StdDeserializer<Customer> {

    public CustomerDeserializer() {
        super(Customer.class);
    }

    @Override
    public Customer deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        Customer customer = new Customer();
        JsonNode treeNode = p.readValueAsTree();
        if (treeNode == null) {
            return null;
        }
        customer.setName(treeNode.get("name").asText());
        customer.setAge(treeNode.get("age").asInt());
        Address address = new Address();
        address.setStreet(treeNode.get("street").asText());
        address.setPostalcode(treeNode.get("postalcode").asText());
        customer.setAddress(address);
        return customer;
    }
}

作为第三选项,您可以使用 @JsonAnySetter 进行某种后期构造处理:

As third option, you can use @JsonAnySetter with some kind of post construct processing:

public interface PostConstruct {
    void postConstruct();
}

public class Customer implements PostConstruct {
    //mapping

    private Map<String, Object> additionalFields = new HashMap<>();

    @JsonAnySetter
    public void setAdditionalValue(String key, Object value) {
        additionalFields.put(key, value);
    }

    @Override
    public void postConstruct() {
        address = new Address();
        address.setStreet(String.valueOf(additionalFields.get("street")));
        address.setPostalcode(String.valueOf(additionalFields.get("postalcode")));
    }
}


public static class PostConstructDeserializer extends DelegatingDeserializer {
    private final JsonDeserializer<?> deserializer;

    public PostConstructDeserializer(JsonDeserializer<?> deserializer) {
        super(deserializer);
        this.deserializer = deserializer;
    }

    @Override
    protected JsonDeserializer<?> newDelegatingInstance(JsonDeserializer<?> newDelegatee) {
        return deserializer;
    }

    @Override
    public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        Object result = _delegatee.deserialize(jp, ctxt);
        if (result instanceof PostConstruct) {
            ((PostConstruct) result).postConstruct();
        }
        return result;
    }
}


//using of post construct deserializer

    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.setDeserializerModifier(new BeanDeserializerModifier() {
        @Override
        public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,
                                                      BeanDescription beanDesc,
                                                      final JsonDeserializer<?> deserializer) {
            return new PostConstructDeserializer(deserializer);
        }
    });
    mapper.registerModule(module);

这篇关于杰克逊:将json的某些字段映射到类的内部字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:26