0

I have the following JSON:

{ "_id" : "3", "longitude" : "3222", "latitude" : "55", "timeDateOfUsage" : ["02/11/17 13:30:35", "1", "02/11/17 13:30:45", "1", "02/11/17 13:30:51", "0"] }

I'm trying to convert it to a pojo using jackson. This is what I have:

String inputJson = "{ \"_id\" : \"3\", \"longitude\" : \"3222\", 

\"latitude\" : \"55\", \"timeDateOfUsage\" : [\"02/11/17 13:30:35\", \"1\", \"02/11/17 13:30:45\", \"1\", \"02/11/17 13:30:51\", \"0\"] }";
        ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY);
        mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        try{
            dbResponseonce  response = mapper.readValue(inputJson,dbResponseonce.class);
            System.out.println(response.getLatitude());
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

And my other class has fields which are named as the elements in the JSON and has getters and setters.

The above code works however it does not work when I just create a basic ObjectMapper object with no configurations. Why is this? This is the stacktrace error:

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "_id" (Class dbResponseonce), not marked as ignorable

This is my pojo class that has the getters and setters:

public class dbResponseonce {

private String _id;

public String getId() { return this._id; }

public void setId(String _id) { this._id = _id; }

private String longitude;

public String getLongitude() { return this.longitude; }

public void setLongitude(String longitude) { this.longitude = longitude; }

private String latitude;

public String getLatitude() { return this.latitude; }

public void setLatitude(String latitude) { this.latitude = latitude; }

private ArrayList<String> timeDateOfUsage;

public ArrayList<String> getTimeDateOfUsage() { return this.timeDateOfUsage; }

public void setTimeDateOfUsage(ArrayList<String> timeDateOfUsage) { this.timeDateOfUsage = timeDateOfUsage; }}

1 Answers1

3

Your getter and setter for _id is incorrect.

Try defining them as follows:

public String get_id() {
    return _id;
}

public void set_id(String _id) {
    this._id = _id;
}

Or use on the getter or setter of _id @JsonProperty("_id")

Andrei Sfat
  • 8,440
  • 5
  • 49
  • 69