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?
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;
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With