java - Map json field from child object -


here description of issue convert json string json.

address{     private long id;     private string city; }  employee{     private long id;     private string name;     private address address; } 

client send request json of employee

{"id": 1, "name": "abc", "address": {"id": 1, "city": "xyz"}} 

now want convert input json below format addressid = id field of address class.

output{     private long id;     private string name;     private long addressid;  } 

is there way achieve this. have tried jackson , gson also.

with jackson, can add class addressid wrap address id field , annotated constructor takes addressid argument. add getters , other constructors needed. doesn't require defining employee , address classes output.

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 case, need configure objectmapper quietly ignore unknown json fields:

mapper.configure(deserializationfeature.fail_on_unknown_properties, false); 

another alternative, doesn't require class addressid, create custom setter addressid field based on map<string, object> jackson uses internally when deserializing. how output in case (add other setters/getters may need):

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

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -