如何使用GSON反序列化嵌套的JSON数组?

3
这里是我的JSON示例:JSON
{
    "status": "ok",
    "rowCount": 60,
    "pageCount": 6,
    "value": [{
        "CustomerID": 1911,
        "CustomerTypeID": 3,
           ...
         }
       ]
}

我的POJO:

@SerializedName("CustomerID")
public Integer CustomerID;

@SerializedName("CustomerTypeID")
public Integer CustomerTypeID;

我想抓取 value 下的所有内容。

如何使用谷歌的 GSON 实现这一点?

我尝试了通常的方法来处理,但由于明显的原因,它没有起作用:

Type collectionType = new TypeToken<ArrayList<Customer>>() {}.getType();
return gson.fromJson(json, collectionType);

https://github.com/google/gson/blob/master/UserGuide.md - Rakhi Dhavale
`GsonBuilder gsonBuilder = new GsonBuilder(); gson = gsonBuilder.create(); ExampleClass resultObj = gson.fromJson(jsonObject.toString(), ExampleClass.class);List<Value> yourListOfCustomerValues = resultObj.getValue();` - Rakhi Dhavale
3个回答

2
你不能跳过根JSON对象。在这种情况下,最简单的解决方案是 - 创建根POJO: "最初的回答"。
class Response {
    @SerializedName("value")
    private List<Customer> customers;

    // getters, setters
}

你可以如下使用它:

并且可以按照以下方式使用:

return gson.fromJson(json, Response.class).getCustomers();

getCustomers() 是从哪里来的? - IgorGanapolsky
1
@IgorGanapolsky,你需要为List<Customer> customers字段创建一个getter方法。 - Michał Ziober

1

您不需要担心编写自己的POJO。

只需访问http://www.jsonschema2pojo.org/ 并将您的JSON数据粘贴到此处,它将自动返回转换后的类,如下所示

-----------------------------------com.example.Example.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;
}

}

-----------------------------------com.example.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;
}

}
上述两个类是由网站自动生成的。

0
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;
  }

 }

-----------------------------------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;
 }

 }

/********* 使用Gson解析 ******/

    GsonBuilder gsonBuilder = new GsonBuilder(); 
    gson = gsonBuilder.create(); 
    ExampleClass resultObj = gson.fromJson(jsonObject.toString(), ExampleClass.class);
    List<Value> yourListOfCustomerValues = resultObj.getValue();

您可以参考Norman Peitek关于使用Gson映射数组和对象列表的精彩文章。

Gson基础知识、模型注解和嵌套对象映射


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接