简体   繁体   English

杰克逊嵌入式Java对象的反序列化

[英]Jackson Deserialization of Embedded Java Object

I have to deserialize following json using Jackson library into Customer class 我必须使用Jackson库将json反序列化为Customer类

{
   "code":"C001",
   "city": "Pune",
   "street": "ABC Road"
}

and Classes as 和类作为

class Address{
    String city;
    String street;
}

class Customer{
    String code;
    Address address;
}

I have found similar question on stack Java jackson embedded object deserialization 我在堆栈Java jackson嵌入式对象反序列化中发现了类似的问题

but answer does not apply to my case. 但答案不适用于我的情况。 Also I only want to use Jackson library. 另外我只想使用杰克逊图书馆。

How can I map this json to Customer object? 如何将此json映射到Customer对象?

You can put a @JsonUnwrapped annotation on the Address field in the customer class. 您可以在客户类的“ Address字段中放置@JsonUnwrapped注释。 Here is an example: 这是一个例子:

public class JacksonValue {
    final static String JSON = "{\n"
            +"   \"code\":\"C001\",\n"
            +"   \"city\": \"Pune\",\n"
            +"   \"street\": \"ABC Road\"\n"
            +"}";

    static class Address {
        public String city;
        public String street;

        @Override
        public String toString() {
            return "Address{" +
                    "city='" + city + '\'' +
                    ", street='" + street + '\'' +
                    '}';
        }
    }

    static class Customer {
        public String code;
        @JsonUnwrapped
        public Address address;

        @Override
        public String toString() {
            return "Customer{" +
                    "code='" + code + '\'' +
                    ", address=" + address +
                    '}';
        }
    }


    public static void main(String[] args) throws IOException {
        final ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.readValue(JSON, Customer.class));
    }
}

Output: 输出:

 Customer{code='C001', address=Address{city='Pune', street='ABC Road'}}

What you need is a custom deserializer. 您需要的是自定义反序列化器。 Jackson How-To: Custom Deserializers Jackson How-To:定制反序列化器

For your use case it could be something like this: 对于您的用例,它可能是这样的:

class CustomerDeserializer extends JsonDeserializer<Customer>
{
  public Customer deserialize(JsonParser p, DeserializationContext ctxt) 
                                        throws IOException, JsonProcessingException
  {
    JsonNode node = p.getCodec().readTree(p);
    String code = node.get("code").asText();
    String city = node.get("city").asText();
    String street = node.get("street").asText();
    Address adr = new Address(city, street);
    return new Customer(code, adr);
  }
}

Your JSON object for a customer should look like this: 您的客户JSON对象应如下所示:

{
   "code":"C001",
   "address":{
        "city": "Pune",
        "street": "ABC Road"
   } 
}

Without some additional transformation this json structure can't be mapped to two classes. 如果没有一些额外的转换,这个json结构就不能映射到两个类。 Either write a class CustomerAddress that will be having all three fields from json and then create Address getAddress() and Customer getCustomer() in it or transform the json to nest the address information inside the customer field as suggested by @eztam. 编写将拥有json中所有三个字段的类CustomerAddress ,然后在其中创建Address getAddress()Customer getCustomer() ,或者转换json以按照@eztam的建议将地址信息嵌套在customer字段中。

public CustomerAddress {
  private String code;
  private String city;
  private String street;

  public Address getAddress() {
    return new Address(city, street);
  }

  public Address getCustomer() {
    return new Customer(code, this.getAddress());
  }
}

Try this !!! 试试这个 !!!

{  
       "code":"customer1",
       "address":{  
          "type":"nested",
          "properties":{  
             "city":"Hyderabad",
             "street":"1000ftRoad"
          }
       }
    }

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

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