user1384991 user1384991 - 2 months ago 18
Java Question

Jackson overcoming underscores in favor of camel-case

I retrieve a JSON string from internet; like most JSON I've seen it includes long keys that are separated by underscores. Essentially, my goal is to deserialize JSON into java-objects, but I don't use underscores in java-code.

For instance, I might have a

User
class with
firstName
field in camel-case, simultaneously I need somehow to tell Jackson to map
first_name
key from JSON to
firstName
class field. Is it possible?

class User{
protected String firstName;
protected String getFirstName(){return firstName;}
}

Answer

You should use the @JsonProperty on the field you want to change the default name mapping.

class User{
    @JsonProperty("first_name")
    protected String firstName;
    protected String getFirstName(){return firstName;}
}

For more info: the API

Comments