unable to parse json string to a generic java object

By : jos
Source: Stackoverflow.com
Question!

I am working on a jersey web services. like this following is the json string i received at my jersey client

{"license":"12345","dimRequirement":[{"alternateId":"AlternateID","classificati‌​on":"1"}] }

but when i am parsing it gives error.I used jackson for parsing

    Unrecognized field "dimRequirement" (Class com.vxl.AppObject), not marked as ignorable
at [Source: [email protected]; line: 1, column: 49] (through reference chain: com.vxl.appanalytix.AppObject["dimRequirement"])
    at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:244)
    at org.codehaus.jackson.map.deser.StdDeserializer.reportUnknownProperty(StdDeserializer.java:589)
    at org.codehaus.jackson.map.deser.StdDeserializer.handleUnknownProperty(StdDeserializer.java:575)
    at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:684)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:515)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:351)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2131)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1402)
    at com.vxl.CheckJersy.main(CheckJersy.java:56)

i tried by adding type Reference for parsing

new ObjectMapper().readValue(
                output,new TypeReference<AppObject<DimRequirement>>() { } );

generic class

    @XmlRootElement
@XmlSeeAlso({ DimRequirement.class})
    public class AppObject<T> implements Serializable {

        private List<T> list;
        private String license;

        public AppObject() {
            list = new ArrayList<T>();

        }

        public AppObject(List<T> list) {
            this.list = list;
        }

        @XmlAnyElement(lax = true)
        public List<T> getList() {
            return list;
        }

        public void setList(List<T> list) {
            this.list = list;
        }

        public String getLicense() {
            return license;
        }

        public void setLicense(String license) {
            this.license = license;
        }

    }

services I want to use the same service for more than one POST request returning AppObject<xxx>,AppObject<yyy>

@POST
    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    @Path("/getreq")
    @Override
    public AppObject getAllByClientNIsCurrent(
            AppObject<DimRequirement> appObject) {
        List<DimRequirement> dimreqlist = dimRequirementDao
                .getAllByClientNIsCurrent(appObject.getClientKey());
        AppObject<DimRequirement> appObject = new AppObject<DimRequirement>();
        appObject.setList(dimreqlist);
        return appObject;
    }
By : jos


Answers

Your AppObject class has a field that is not present in the JSON. You need to make the class with a jackson annotation to say it can be ignored.

By : tom


Sorry, about my other answer, please ignore it.

I got your code working exactly as is. All that was needed was the variable "list" needs to be named "dimRequirement" for the auto-mapping to work. You will also need to amend the getters and setters for this, as some Jackson settings can make the library use them.



This video can help you solving your question :)
By: admin