Search code examples
javajsonjacksonpojo

Json to pojo classes mapping with jackson


My json response is as follows

{"repositories": {

   "link":    [
            {
         "@rel": "self",
         "$": "http://localhost/content/repository"
      },
            {
         "@rel": "repositories",
         "$": "http://localhost/path/repository"
      }
   ]

}
}

I want the value of @rel in one of my variable in java.

I know how to create corresponding POJO classes for the JSON response and extract the values from it using Jackson library.I have created POJOs for quite a few json responses.But in this particular response i'm stuck because i have to create variable named "@rel" in my java class. but we can't create variable name starting with '@' in java(Or is der any way to do so which i don't know).

So how we can extract the value of @rel ?

the response can't change. I have to do some manipulation for extraction.

Please help.


Solution

  • Use the @JsonProperty annotation:

    @JsonProperty("@rel")
    private String rel;
    

    You can also use a Map<String, String> as a raw type.