Can't deserialize JSON to POJO (using Jackson)

I am using Jackson

to deserialize the following JSON

in POJO:

{
    "etag": "\"SomeETag\"",
    "jobComplete": true,
    "jobReference": {
        "jobId": "someJobId",
        "projectId": "someProjectId"
    },
    "rows": [
        {
            "f": [
                {
                    "v": "101"
                },
                {
                    "v": "FirstService"
                },
                {
                    "v": "firstAPI"
                }
            ]
        },
        {
            "f": [
                {
                    "v": "102"
                },
                {
                    "v": "SecondService"
                },
                {
                    "v": "SecondAPI"
                }
            ]
        }
    ],
    "totalRows": "2"
}

      

Here's the POJO I'm using:

    @JsonAutoDetect
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Response {

        public String etag;
        public boolean jobComplete;
        public List<FWrapper> rows = new ArrayList<FWrapper>();
    }

    class FWrapper {
        public F f; 
    }

    class F {
        public List<VWrapper> vWrappers = new ArrayList<VWrapper>();       
    }

    class VWrapper {
        public String v;
    }

      

I am using this piece of code to display:

    ObjectMapper mapper = new ObjectMapper();
    Response response = mapper.readValue(response, Response.class);

      

And here is the error I am getting:

org.codehaus.jackson.map.JsonMappingException

: Unable to deserialize mypackage.F instance from START_ARRAY token to [Source: java.io.StringReader@11563ff ; line: 1, column: 227] (via link chain: mypackage.Response ["rows"] → mypackage.FWrapper ["f"])

I've checked several old questions with a similar error, but haven't been able to resolve mine yet. This one looks very similar to mine as the correct answer says Your data is problematic in that you have inner wrapper objects in your array

which I think also applies to my data. But still I was unable to do the correct POJO mapping. Any pointers would be appreciated.

EDIT: Fixed typo ( JSON

I am using valid)

+3


source to share


2 answers


Your class model doesn't match your JSON.

In your JSON, f is an array, not an object. Your F class has an object that contains a list of things.



Mapping like this should work (untested)

public class Response {

    public String etag;
    public boolean jobComplete;
    public List<F> rows;
}


public class F {
    public List<V> f;
}

public Class V {
    public String v;
}

      

+5


source


Your "rows" array does not have a closing square bracket ] .

This makes the JSON invalid.

Read the error message carefully:

org.codehaus.jackson.map.JsonMappingException: Unable to deserialize mypackage.F instance from START_ARRAY marker to [Source: java.io.StringReader@11563ff ; line: 1, column: 227] (via link chain: mypackage.Response [ "rows" ] → mypackage.FWrapper ["f"])




Now I see that you changed the JSON - added it.

Do you have the same problem?

0


source







All Articles