如何在Retrofit中迭代键值对

时间:2017-04-23 05:49:10

标签: java android json retrofit2

我在解析下面的JSON结构时遇到问题。基本上我必须将值对象作为列表读取,但服务器返回JsonObject并且值根据totalPageCount更改。有什么方法可以将值读作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;
}

}

1 个答案:

答案 0 :(得分:5)

这将有效

Script.py