4

I'm testing/creating a REST client to the new Basecamp API using Retrofit. It looks something like this:

class Project {
  String name;
  String appUrl;
}

interface Basecamp {
    @GET("/projects.json")
    List<Project> projects();
}

In the json response, the source field for appUrl is called app_url. Asides from renaming the field in the class, is there a simple way to map the response data with my data structure?

Mitkins
  • 4,031
  • 3
  • 40
  • 77

2 Answers2

12

So I found the answer in this question. Turns out this can be solved using Gson:

class Project {
  String name;

  @SerializedName("app_url")
  String appUrl;
}
Community
  • 1
  • 1
Mitkins
  • 4,031
  • 3
  • 40
  • 77
3

If you are using Jackson for JSON parsing with Retrofit you can use the @JsonProperty annotation:

Example:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties (ignoreUnknown = true)
    class Project {
      @JsonProperty("name")
      String name;
      @JsonProperty("app_url")
      String appUrl;
    }
Patrick Dorn
  • 756
  • 8
  • 13