9

I am using jackson json api to map json data to java objects. All is well in case of same object attribute names with json attributes. Now i have a situation where i am getting json data attribute with -. (my-name).

In java we can't include - in variable names.

import org.codehaus.jackson.map.ObjectMapper;

private static final ObjectMapper mapper = new ObjectMapper();

User user = mapper.readValue("{my-name:\"abcd\"}", User.class);

public class User {private String my_name; /*get-set methods*/}

Is there anything i need to apply in User.class.

I don't want to change my code so much.

Satish Pandey
  • 1,184
  • 4
  • 12
  • 32

1 Answers1

19

In your java class you can give any name as you like

Ex. private String myName;

But in the setter method just write:

@JsonProperty("my-name")
public void setMyName(String myName) {
    this.myName = myName;
}
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Ajit
  • 215
  • 2
  • 8