Cannot populate POJO with JSON using Jackson -
i'm trying populate pojo json doesn't match in way , having trouble getting resolved. can't change json since external service maybe able modify pojo if needed.
below example json:
{"sparse":[{"pixid":1,"pixname":"swe","description":"unknown"},{"pixid":2,"pixname":"pumnw","description":"power supplement"}],"status":0,"message":null}
below pojo:
@jsonignoreproperties(ignoreunknown = true) public class pix { @jsonproperty("description") private string description; @jsonproperty("pixid") private int pixid; @jsonproperty("pixname") private string pixname; // getters , setters }
and here code conversion:
objectmapper om = new objectmapper(); om.configure(deserializationconfig.feature.accept_single_value_as_array, true); om.configure(deserializationconfig.feature.fail_on_unknown_properties, false); list<pix> pixlist = om.readvalue(pixjson, new typereference<list<pix>>() {});
the pixlist contains 1 element (should 2 using json above) , properties have not been populated. i'm using jackson 1.9.9. ideas on how work? tia.
you have create new pojo class main object contains list<pix>
. looks this:
class root { @jsonproperty("status") private int status; @jsonproperty("message") private string message; @jsonproperty("sparse") private list<pix> sparse; //getters/setters }
and deserialization code looks this:
objectmapper mapper = new objectmapper(); mapper.configure(deserializationfeature.accept_single_value_as_array, true); mapper.configure(deserializationfeature.fail_on_unknown_properties, false); list<pix> pixlist = mapper.readvalue(pixjson, root.class).getsparse();
Comments
Post a Comment