我正在使用改造/ okhttp。我通过改造搜索了一些关于处理嵌套JSON对象的问题,并得到了我的答案from here,根据结果使用了几个POJO类。
这是一种简单的方法,我可以根据我需要的元素直接解析JSON对象吗?
例如我有一个JSON结果
{
"DealsViewModel": {
"CountryDealsViewModel": {
"CultureCountry": "string",
"CultureLanguage": "string",
"DealsPageInfo": [
{
"DisplayName": "string",
"StartDate": "2018-05-07T06:43:31.179Z",
"ExpiredDate": "2018-05-07T06:43:31.179Z",
"Url": "string",
"ImageUrl": "string",
"ShortDescription": "string"
}
],
"CountryCode": "string",
"CountryName": "string"
}
},
"Status": 0,
"Code": 0,
"Message": "string"
}
从结果来看,如果按照上一个答案的方法,我需要有大约4个POJO文件来处理结果,但我只需要“状态”“代码”“消息”和“ DealsPageInfo“
这是否可以让我有这样的东西?
public class ExclusiveOffersParent {
private List<ExclusiveOffers> DealsPageInfo;
private String Message;
private int Status;
private int Code;
public ExclusiveOffersParent() {
this.DealsPageInfo = new ArrayList<>();
}
public List<ExclusiveOffers> getDeals() {
return DealsPageInfo;
}
public void setDeals(List<ExclusiveOffers> deals) {
DealsPageInfo = deals;
}
public String getMessage() {
return Message;
}
public void setMessage(String message) {
Message = message;
}
public int getStatus() {
return Status;
}
public void setStatus(int status) {
Status = status;
}
public int getCode() {
return Code;
}
public void setCode(int code) {
Code = code;
}
}
答案 0 :(得分:0)
你必须为所有孩子制作pojo:
将json粘贴到方框
插入包名和类名
从源类型中选择JSON
从注释样式中选择Gson
按预览
这将为您提供反序列化json响应所需的所有pojos。 您还需要在gradle文件中导入gson,在依赖项中添加以下行:
compile 'com.google.code.gson:gson:2.6.2'
答案 1 :(得分:0)
private void getResponseList() {
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<Example> call = apiService.getResponse();
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(@NonNull Call<Example> call, @NonNull Response<Example> response) {
assert response.body() != null;
Log.e("Message",""+response.body().getMessage());
Log.e("Code",""+response.body().getCode());
Log.e("DisplayName",""+response.body().getDealsViewModel().getCountryDealsViewModel().getDealsPageInfo().get(0).getExpiredDate());
}
@Override
public void onFailure(@NonNull Call<Example> call, @NonNull Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
});
}
===============================
public class CountryDealsViewModel {
@SerializedName("CultureCountry")
@Expose
private String cultureCountry;
@SerializedName("CultureLanguage")
@Expose
private String cultureLanguage;
@SerializedName("DealsPageInfo")
@Expose
private List<DealsPageInfo> dealsPageInfo = null;
@SerializedName("CountryCode")
@Expose
private String countryCode;
@SerializedName("CountryName")
@Expose
private String countryName;
public String getCultureCountry() {
return cultureCountry;
}
public void setCultureCountry(String cultureCountry) {
this.cultureCountry = cultureCountry;
}
public String getCultureLanguage() {
return cultureLanguage;
}
public void setCultureLanguage(String cultureLanguage) {
this.cultureLanguage = cultureLanguage;
}
public List<DealsPageInfo> getDealsPageInfo() {
return dealsPageInfo;
}
public void setDealsPageInfo(List<DealsPageInfo> dealsPageInfo) {
this.dealsPageInfo = dealsPageInfo;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
}
==================================
public class DealsPageInfo {
@SerializedName("DisplayName")
@Expose
private String displayName;
@SerializedName("StartDate")
@Expose
private String startDate;
@SerializedName("ExpiredDate")
@Expose
private String expiredDate;
@SerializedName("Url")
@Expose
private String url;
@SerializedName("ImageUrl")
@Expose
private String imageUrl;
@SerializedName("ShortDescription")
@Expose
private String shortDescription;
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getExpiredDate() {
return expiredDate;
}
public void setExpiredDate(String expiredDate) {
this.expiredDate = expiredDate;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
}
===============================
公共类DealsViewModel {
@SerializedName("CountryDealsViewModel")
@Expose
private CountryDealsViewModel countryDealsViewModel;
public CountryDealsViewModel getCountryDealsViewModel() {
return countryDealsViewModel;
}
public void setCountryDealsViewModel(CountryDealsViewModel countryDealsViewModel) {
this.countryDealsViewModel = countryDealsViewModel;
}
}
=============================
public class Example {
@SerializedName("DealsViewModel")
@Expose
private DealsViewModel dealsViewModel;
@SerializedName("Status")
@Expose
private Integer status;
@SerializedName("Code")
@Expose
private Integer code;
@SerializedName("Message")
@Expose
private String message;
public DealsViewModel getDealsViewModel() {
return dealsViewModel;
}
public void setDealsViewModel(DealsViewModel dealsViewModel) {
this.dealsViewModel = dealsViewModel;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}