Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson XML: nested field deserialization

Tags:

java

xml

jackson

I have the following xml

<MyPojo>
  <name>Jason</name>
  <age>25</age>
  <meta>
    <occupation>Engineer</occupation>
  </meta>
</MyPojo>

I need to deserialize it to the following POJO:

public class MyPojo {

    private String name;
    private int age;
    private String occupation;
}

The problem here is that occupation is wrapped within meta element

like image 755
user1745356 Avatar asked Dec 17 '25 20:12

user1745356


2 Answers

You need one more object:

public class MyPojo {

    private String name;
    private int age;
    private Meta meta;
}

public class Meta{
    private String occupation;
}
like image 127
Jens Avatar answered Dec 20 '25 11:12

Jens


My idea is to replace occupation with an own class. Something like myMeta or whatever you want to call it(be aware in your case like the xml says: meta). This class should cotain the field occupation:

   public class Meta
   {
       private String occupation;
   }

After that you only have to add a new field of your new class e.g. myMeta to myPojo. Something like this:

   public class MyPojo
   {
       private String name;
       private int age;
       private Meta meta;
   }

this should avoid

that occupation is wrapped within meta element

Hope that helps!

like image 24
Felix Gerber Avatar answered Dec 20 '25 09:12

Felix Gerber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!