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

时间:2019-03-31 20:30:52

标签: java android json gson

以下是我的JSON的示例:

{
    "status": "ok",
    "rowCount": 60,
    "pageCount": 6,
    "value": [{
        "CustomerID": 1911,
        "CustomerTypeID": 3,
           ...
         }
       ]
}

我的POJO:

@SerializedName("CustomerID")
public Integer CustomerID;

@SerializedName("CustomerTypeID")
public Integer CustomerTypeID;

我想将所有内容都放在value下。

如何使用Google的GSON来做到这一点?

我已经像往常一样尝试过,但是出于明显的原因,它没有用:

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

3 个答案:

答案 0 :(得分:0)

您不能跳过根JSON object。在这种情况下,最简单的解决方案是-创建根POJO

class Response {
    @SerializedName("value")
    private List<Customer> customers;

    // getters, setters
}

您可以按以下方式使用它:

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

答案 1 :(得分: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数组和对象列表的映射的精彩文章

Basics of Gson, model annotations and mapping of nested objects

答案 2 :(得分:0)

您不必担心编写自己的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;
}

}

以上两个类是网站自动生成的。