我在解析以下JSON结构时遇到了麻烦。基本上,我需要将values对象读取为列表,但服务器返回的是一个JsonObject
,并且根据totalPageCount
值而改变其值。有什么办法可以将values作为List
读取吗?我应该使用反射吗?目前我正在使用返回模型类的Retrofit。
非常感谢任何帮助。
谢谢
{
"page" : 0,
"pageSize" : 10,
"totalPageCount" : 1,
"values" : {
"key1" : "value1",
"key2" : "value2",
"key3" : "value3",
"key4" : "value4",
}
}
模型类:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("page")
@Expose
private Integer page;
@SerializedName("pageSize")
@Expose
private Integer pageSize;
@SerializedName("totalPageCount")
@Expose
private Integer totalPageCount;
@SerializedName("values")
@Expose
private Values values;
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalPageCount() {
return totalPageCount;
}
public void setTotalPageCount(Integer totalPageCount) {
this.totalPageCount = totalPageCount;
}
public Values getValues() {
return values;
}
public void setValues(Values values) {
this.values = values;
}
}
-----------------------------------com.example.Values.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Values {
@SerializedName("key1")
@Expose
private String key1;
@SerializedName("key2")
@Expose
private String key2;
@SerializedName("key3")
@Expose
private String key3;
@SerializedName("key4")
@Expose
private String key4;
public String getKey1() {
return key1;
}
public void setKey1(String key1) {
this.key1 = key1;
}
public String getKey2() {
return key2;
}
public void setKey2(String key2) {
this.key2 = key2;
}
public String getKey3() {
return key3;
}
public void setKey3(String key3) {
this.key3 = key3;
}
public String getKey4() {
return key4;
}
public void setKey4(String key4) {
this.key4 = key4;
}
}