3 回答
TA贡献1847条经验 获得超11个赞
您不能跳过根目录 。在这种情况下,最简单的解决方案是 - 创建根:JSON objectPOJO
class Response {
@SerializedName("value")
private List<Customer> customers;
// getters, setters
}
您可以按如下方式使用它:
return gson.fromJson(json, Response.class).getCustomers();
TA贡献1828条经验 获得超13个赞
您不必担心编写自己的POJO。
只需访问 http://www.jsonschema2pojo.org/ 并将您的JSON数据粘贴到此处,它将自动返回您转换后的类,如下所示
-----------------------------------示例.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("status")
@Expose
private String status;
@SerializedName("rowCount")
@Expose
private Integer rowCount;
@SerializedName("pageCount")
@Expose
private Integer pageCount;
@SerializedName("value")
@Expose
private List<Value> value = null;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Integer getRowCount() {
return rowCount;
}
public void setRowCount(Integer rowCount) {
this.rowCount = rowCount;
}
public Integer getPageCount() {
return pageCount;
}
public void setPageCount(Integer pageCount) {
this.pageCount = pageCount;
}
public List<Value> getValue() {
return value;
}
public void setValue(List<Value> value) {
this.value = value;
}
}
-----------------------------------.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Value {
@SerializedName("CustomerID")
@Expose
private Integer customerID;
@SerializedName("CustomerTypeID")
@Expose
private Integer customerTypeID;
public Integer getCustomerID() {
return customerID;
}
public void setCustomerID(Integer customerID) {
this.customerID = customerID;
}
public Integer getCustomerTypeID() {
return customerTypeID;
}
public void setCustomerTypeID(Integer customerTypeID) {
this.customerTypeID = customerTypeID;
}
}
以上两类都是网站自动生成的。
TA贡献1806条经验 获得超5个赞
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ExampleClass {
@SerializedName("status")
@Expose
private String status;
@SerializedName("rowCount")
@Expose
private int rowCount;
@SerializedName("pageCount")
@Expose
private int pageCount;
@SerializedName("value")
@Expose
private List<Value> value = null;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public List<Value> getValue() {
return value;
}
public void setValue(List<Value> value) {
this.value = value;
}
}
-----------------------------------价值.java-----------------------------------
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Value {
@SerializedName("CustomerID")
@Expose
private int customerID;
@SerializedName("CustomerTypeID")
@Expose
private int customerTypeID;
public int getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public int getCustomerTypeID() {
return customerTypeID;
}
public void setCustomerTypeID(int customerTypeID) {
this.customerTypeID = customerTypeID;
}
}
/****** 解析与格森 ******/
GsonBuilder gsonBuilder = new GsonBuilder();
gson = gsonBuilder.create();
ExampleClass resultObj = gson.fromJson(jsonObject.toString(), ExampleClass.class);
List<Value> yourListOfCustomerValues = resultObj.getValue();
您可以参考这篇关于诺曼·佩特克的Gson的数组映射和对象列表的惊人帖子
添加回答
举报