Json with '@attr' in name to java POJO

I have the following json schema. The schema has a @ character in the schema. How does this translate to a java pojo?

Sample json

{
"@type":"employee",
"name":"John Doe"
}

2 answers

  • answered 2017-11-14 23:45 Ian

    You can use the Jackson Library and do something like:

    public class Person {
    
        private final String type;
    
        private final String name;
    
        @JsonCreator
        public Person(@JsonProperty("@type") String type, @JsonProperty("name") String name) {
            this.type = type;
            this.name = name;
        }
    
        public String getType() {
            return type;
        }
    
        public String getName() {
            return name;
        }
    }
    

  • answered 2017-11-14 23:51 nafas

    Gson is another alternative:

    public class Person{
      @SerializedName("@type")
      public String type;
    
      public String name;
    }
    

    then you can use it as :

    Person person = new Gson().fromJson(YOURJSON,Person.class);