【发布时间】:2015-06-19 13:49:07
【问题描述】:
我正在使用 Robospice + Retrofit + Jackson。我没有普通类,它有另一个类对象作为字段。我需要解析 json 并使用字段创建类。
这是我的课
@JsonIgnoreProperties(ignoreUnknown=true)
public class User implements UserInformationProvider {
@JsonProperty("customer_id")
public int id;
@JsonProperty("firstname")
public String firstName;
@JsonProperty("lastname")
public String lastName;
@JsonProperty("email")
public String email;
@JsonProperty("telephone")
public String phone;
@JsonProperty("token_api")
public String token;
@JsonProperty("token_expire")
public int tokenExpireTime;
public UserPreferences userPreferences;
@Override
public String getUserFirstName() {
return firstName;
}
@Override
public String getUserLastName() {
return lastName;
}
@Override
public String getUserEmail() {
return email;
}
@Override
public String getUserIconUrl() {
return null;
}
}
和偏好类
public class UserPreferences {
public boolean offersNotifications;
public boolean statusChangedNotifications;
public boolean subscriptionNotifications;
@JsonProperty("new_offers")
public boolean newOffersNotify;
@JsonProperty("order_status_changed")
public boolean orderStatusChangedNotify;
@JsonProperty("hot_offers")
public boolean hotOffersNotify;
}
我需要解析成 POJO 的请求。
{
"customer_id": 84,
"token_api": "ef5d7d2cd5dfa27a",
"token_expire_unix": "1435113663",
"preferences": {
"new_offers": "1",
"order_status_changed": "1",
"hot_offers": "1"
}
}
请帮忙,我如何使用 Jackson 来做到这一点。如果有任何帮助,我将不胜感激。提前致谢。
【问题讨论】: