我正在反序列化来自api的json响应,我有一个有效的代码,但是我唯一关心的是Object
类中的元素,如何完全消除Object类并仅返回元素直接来自ListUsers
类?
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class ListUsers{
@SerializedName("meta")
@Expose
private Meta meta;
@SerializedName("objects")
@Expose
private List<Objects> objects = null;
public Meta getMeta() {
return meta;
}
public void setMeta(Meta meta) {
this.meta = meta;
}
public List<Objects> getObjects() {
return objects;
}
public void setObjects(List<Objects> objects) {
this.objects = objects;
}
private class Meta {
@SerializedName("limit")
@Expose
private int limit;
@SerializedName("offset")
@Expose
private int offset;
@SerializedName("total_count")
@Expose
private int totalCount;
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Meta)) return false;
Meta meta = (Meta) o;
return getLimit() == meta.getLimit() &&
getOffset() == meta.getOffset() &&
getTotalCount() == meta.getTotalCount();
}
@Override
public int hashCode() {
return java.util.Objects.hash(getLimit(), getOffset(), getTotalCount());
}
@Override
public String toString() {
return "Meta{" +
"limit=" + limit +
", offset=" + offset +
", totalCount=" + totalCount +
'}';
}
}
private class Objects {
@SerializedName("address_one")
@Expose
public String addressOne;
@SerializedName("address_three")
@Expose
public String addressThree;
@SerializedName("address_two")
@Expose
public String addressTwo;
@SerializedName("country_code")
@Expose
public String countryCode;
@SerializedName("cs_domain_id")
@Expose
public String getAddressOne() {
return addressOne;
}
public void setAddressOne(String addressOne) {
this.addressOne = addressOne;
}
public String getAddressThree() {
return addressThree;
}
public void setAddressThree(String addressThree) {
this.addressThree = addressThree;
}
public String getAddressTwo() {
return addressTwo;
}
public void setAddressTwo(String addressTwo) {
this.addressTwo = addressTwo;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
}
}
答案 0 :(得分:1)
写一个Adapter (this is a wikipedia link)。
Objects
的列表)
是一种有趣且富有创造力的方式(也许是getAddressLine1
方法之类。)根据适配器的使用方式,
您可以将List<Objects>
解析为Map<Objects>
,以方便使用。